LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / compilerplugins / clang / pluginhandler.hxx
blobf7a399672c5da77e333681080595431776668a6b
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 private:
72 void handleOption( const std::string& option );
73 void createPlugins( std::set< std::string > rewriters );
74 DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc = SourceLocation());
75 bool checkIgnoreLocation(SourceLocation loc);
76 CompilerInstance& compiler;
77 StringRef const mainFileName;
78 std::unordered_map<SourceLocation, bool> ignored_;
79 Rewriter rewriter;
80 std::string scope;
81 std::string warningsOnly;
82 bool warningsAsErrors;
83 bool debugMode = false;
84 std::vector<std::pair<char const*, char const*>> mvModifiedRanges;
86 // Used internally by isAllRelevantCodeDefined:
87 RecordCompleteMap RecordsComplete_;
88 RecordCompleteMap MNCComplete_;
91 /**
92 The Clang plugin class, just forwards to PluginHandler.
94 class LibreOfficeAction
95 : public PluginASTAction
97 public:
98 virtual std::unique_ptr<ASTConsumer> CreateASTConsumer( CompilerInstance& Compiler, StringRef InFile );
100 virtual bool ParseArgs( const CompilerInstance& CI, const std::vector< std::string >& args );
101 private:
102 std::vector< std::string > _args;
107 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */