libreoffice/compilerplugins/clang/pluginhandler.hxx
Stephan Bergmann 32c31c03d0 New --enable-compiler-plugins=debug mode
...to enable debug-only code in the plugins.  Some situations in the plugin code
should never happen, yet must not by default report errors or trigger
assertions, as some newly written LO code could trigger them nevertheless (in
which case the plugin code will likely need to be adapted, to cater for these
presumed-impossible situations).

Such code can now be included in the plugins behind an if(isDebugMode()) guard,
and can explicitly be enabled with --enable-compiler-plugins=debug.

I deliberately made this a runtime rather than a compile time option (using
some #ifdef guards in the plugin code, say), as it IMO keeps the code more
readable, and also allows overridding COMPILER_PLUGINS_DEBUG=... on the make
command line.

Change-Id: Iea4f0c2783ad968a0de097fa710b3be1a248de73
Reviewed-on: https://gerrit.libreoffice.org/46096
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2017-12-08 21:09:39 +01:00

99 lines
2.8 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* Based on LLVM/Clang.
*
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
*
*/
#ifndef PLUGINHANDLER_H
#define PLUGINHANDLER_H
#include <cstddef>
#include <functional>
#include <memory>
#include <set>
#include <unordered_map>
#include <clang/AST/ASTConsumer.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendAction.h>
#include <clang/Rewrite/Core/Rewriter.h>
using namespace clang;
namespace std {
template<> struct hash<::clang::SourceLocation> {
size_t operator ()(::clang::SourceLocation loc) const
{ return loc.getRawEncoding(); }
};
}
namespace loplugin
{
class Plugin;
struct InstantiationData;
/**
Class that manages all LO modules.
*/
class PluginHandler
: public ASTConsumer
{
public:
PluginHandler( CompilerInstance& compiler, const std::vector< std::string >& args );
virtual ~PluginHandler();
virtual void HandleTranslationUnit( ASTContext& context ) override;
static void registerPlugin( Plugin* (*create)( const InstantiationData& ), const char* optionName, bool isPPCallback, bool byDefault );
DiagnosticBuilder report( DiagnosticsEngine::Level level, const char * plugin, StringRef message,
CompilerInstance& compiler, SourceLocation loc = SourceLocation());
bool ignoreLocation(SourceLocation loc);
bool addRemoval( SourceLocation loc );
bool isDebugMode() const { return debugMode; }
static bool isUnitTestMode();
private:
void handleOption( const std::string& option );
void createPlugins( std::set< std::string > rewriters );
DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc = SourceLocation());
bool checkIgnoreLocation(SourceLocation loc);
CompilerInstance& compiler;
StringRef const mainFileName;
std::unordered_map<SourceLocation, bool> ignored_;
Rewriter rewriter;
std::set< SourceLocation > removals;
std::string scope;
std::string warningsOnly;
bool warningsAsErrors;
bool debugMode = false;
};
/**
The Clang plugin class, just forwards to PluginHandler.
*/
class LibreOfficeAction
: public PluginASTAction
{
public:
#if CLANG_VERSION >= 30600
virtual std::unique_ptr<ASTConsumer> CreateASTConsumer( CompilerInstance& Compiler, StringRef InFile );
#else
virtual ASTConsumer* CreateASTConsumer( CompilerInstance& Compiler, StringRef InFile );
#endif
virtual bool ParseArgs( const CompilerInstance& CI, const std::vector< std::string >& args );
private:
std::vector< std::string > _args;
};
} // namespace
#endif // COMPILEPLUGIN_H
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */