Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / pluginhandler.hxx
blobe2ca4075d33d682354c0fee443c635476b5a07db
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * Based on LLVM/Clang.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
12 #pragma once
14 #include <cstddef>
15 #include <functional>
16 #include <memory>
17 #include <set>
18 #include <unordered_map>
20 #include <clang/AST/ASTConsumer.h>
21 #include <clang/Frontend/CompilerInstance.h>
22 #include <clang/Frontend/FrontendAction.h>
23 #include <clang/Rewrite/Core/Rewriter.h>
25 using namespace clang;
27 namespace std {
29 template<> struct hash<::clang::SourceLocation> {
30 size_t operator ()(::clang::SourceLocation loc) const
31 { return loc.getRawEncoding(); }
36 namespace loplugin
39 class Plugin;
40 struct InstantiationData;
42 // Used internally by PluginHandler::isAllRelevantCodeDefined and its (free) helper functions:
43 typedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap;
45 /**
46 Class that manages all LO modules.
48 class PluginHandler
49 : public ASTConsumer
51 public:
52 PluginHandler( CompilerInstance& compiler, const std::vector< std::string >& args );
53 virtual ~PluginHandler();
54 virtual void HandleTranslationUnit( ASTContext& context ) override;
55 static void registerPlugin( Plugin* (*create)( const InstantiationData& ), const char* optionName,
56 bool isPPCallback, bool isSharedPlugin, bool byDefault );
57 DiagnosticBuilder report( DiagnosticsEngine::Level level, const char * plugin, StringRef message,
58 CompilerInstance& compiler, SourceLocation loc = SourceLocation());
59 bool ignoreLocation(SourceLocation loc);
60 bool isDebugMode() const { return debugMode; }
61 static bool isUnitTestMode();
62 // If we overlap with a previous area we modified, we cannot perform this change
63 // without corrupting the source
64 bool checkOverlap(SourceRange range);
65 void addSourceModification(SourceRange range);
66 StringRef const& getMainFileName() const { return mainFileName; }
68 // Is all code that could see `decl` defined in this TU?
69 bool isAllRelevantCodeDefined(NamedDecl const * decl);
71 void enableTreeWideAnalysisMode() { treeWideAnalysisMode = true; }
73 private:
74 void handleOption( const std::string& option );
75 void createPlugins( std::set< std::string > rewriters );
76 DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc = SourceLocation());
77 bool checkIgnoreLocation(SourceLocation loc);
78 CompilerInstance& compiler;
79 StringRef const mainFileName;
80 std::unordered_map<SourceLocation, bool> ignored_;
81 Rewriter rewriter;
82 std::string scope;
83 std::string warningsOnly;
84 bool warningsAsErrors;
85 bool debugMode = false;
86 //// Used by the tree-wide analysis plugins like unusedmethods, etc.
87 bool treeWideAnalysisMode = false;
88 std::vector<std::pair<char const*, char const*>> mvModifiedRanges;
90 // Used internally by isAllRelevantCodeDefined:
91 RecordCompleteMap RecordsComplete_;
92 RecordCompleteMap MNCComplete_;
95 /**
96 The Clang plugin class, just forwards to PluginHandler.
98 class LibreOfficeAction
99 : public PluginASTAction
101 public:
102 virtual std::unique_ptr<ASTConsumer> CreateASTConsumer( CompilerInstance& Compiler, StringRef InFile );
104 virtual bool ParseArgs( const CompilerInstance& CI, const std::vector< std::string >& args );
105 private:
106 std::vector< std::string > _args;
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */