1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
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
;
29 template<> struct hash
<::clang::SourceLocation
> {
30 size_t operator ()(::clang::SourceLocation loc
) const
31 { return loc
.getRawEncoding(); }
40 struct InstantiationData
;
42 // Used internally by PluginHandler::isAllRelevantCodeDefined and its (free) helper functions:
43 typedef llvm::DenseMap
<const CXXRecordDecl
*, bool> RecordCompleteMap
;
46 Class that manages all LO modules.
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; }
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_
;
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_
;
96 The Clang plugin class, just forwards to PluginHandler.
98 class LibreOfficeAction
99 : public PluginASTAction
102 virtual std::unique_ptr
<ASTConsumer
> CreateASTConsumer( CompilerInstance
& Compiler
, StringRef InFile
);
104 virtual bool ParseArgs( const CompilerInstance
& CI
, const std::vector
< std::string
>& args
);
106 std::vector
< std::string
> _args
;
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */