update credits
[LibreOffice.git] / compilerplugins / clang / plugin.hxx
blob9c9ce7b72f558f79f992b24e6872109b516761b8
1 /*
2 * This file is part of the LibreOffice project.
4 * Based on LLVM/Clang.
6 * This file is distributed under the University of Illinois Open Source
7 * License. See LICENSE.TXT for details.
9 */
11 #ifndef PLUGIN_H
12 #define PLUGIN_H
14 #include <config_clang.h>
16 #include <clang/AST/ASTContext.h>
17 #include <clang/AST/RecursiveASTVisitor.h>
18 #include <clang/Basic/FileManager.h>
19 #include <clang/Basic/SourceManager.h>
20 #include <clang/Frontend/CompilerInstance.h>
21 #include <set>
23 #if __clang_major__ < 3 || __clang_major__ == 3 && __clang_minor__ < 2
24 #include <clang/Rewrite/Rewriter.h>
25 #else
26 #include <clang/Rewrite/Core/Rewriter.h>
27 #endif
29 using namespace clang;
30 using namespace llvm;
31 using namespace std;
33 namespace loplugin
36 /**
37 Base class for plugins.
39 If you want to create a non-rewriter action, inherit from this class. Remember to also
40 use Plugin::Registration.
42 class Plugin
44 public:
45 explicit Plugin( CompilerInstance& compiler );
46 virtual ~Plugin();
47 virtual void run() = 0;
48 template< typename T > class Registration;
49 DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc = SourceLocation());
50 static DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message,
51 CompilerInstance& compiler, SourceLocation loc = SourceLocation());
52 protected:
53 bool ignoreLocation( SourceLocation loc );
54 bool ignoreLocation( const Decl* decl );
55 bool ignoreLocation( const Stmt* stmt );
56 CompilerInstance& compiler;
57 private:
58 static void registerPlugin( Plugin* (*create)( CompilerInstance&, Rewriter& ), const char* optionName, bool isRewriter );
59 template< typename T > static Plugin* createHelper( CompilerInstance& compiler, Rewriter& rewriter );
60 enum { isRewriter = false };
63 /**
64 Base class for rewriter plugins.
66 Remember to also use Plugin::Registration.
68 class RewritePlugin
69 : public Plugin
71 public:
72 explicit RewritePlugin( CompilerInstance& compiler, Rewriter& rewriter );
73 protected:
74 // This enum allows passing just 'RemoveLineIfEmpty' to functions below.
75 enum RemoveLineIfEmpty_t { RemoveLineIfEmpty };
76 // Use this to remove the declaration/statement as a whole, i.e. all whitespace before the statement
77 // and the trailing semicolor (is not part of the AST element range itself).
78 // The trailing semicolon must be present.
79 enum RemoveWholeStatement_t { RemoveWholeStatement };
80 enum RemoveLineIfEmptyAndWholeStatement_t { RemoveLineIfEmptyAndWholeStatement };
81 // syntactic sugar to be able to write 'RemoveLineIfEmpty | RemoveWholeStatement'
82 friend RemoveLineIfEmptyAndWholeStatement_t operator|( RemoveLineIfEmpty_t, RemoveWholeStatement_t )
83 { return RemoveLineIfEmptyAndWholeStatement; }
84 struct RewriteOptions
85 : public Rewriter::RewriteOptions
87 RewriteOptions() : RemoveWholeStatement( false ) {} // default
88 RewriteOptions( RemoveLineIfEmpty_t ) : RemoveWholeStatement( false ) { RemoveLineIfEmpty = true; }
89 RewriteOptions( RemoveWholeStatement_t ) : RemoveWholeStatement( true ) {}
90 RewriteOptions( RemoveLineIfEmptyAndWholeStatement_t ) : RemoveWholeStatement( true ) { RemoveLineIfEmpty = true; }
91 bool RemoveWholeStatement;
93 // These following insert/remove/replaceText functions map to functions
94 // in clang::Rewriter, with these differences:
95 // - they (more intuitively) return false on failure rather than true
96 // - they report a warning when the change cannot be done
97 // - There is RemoveWholeStatement to also remove the trailing semicolon when removing (must be there)
98 // and al preceding whitespace.
99 bool insertText( SourceLocation Loc, StringRef Str,
100 bool InsertAfter = true, bool indentNewLines = false );
101 bool insertTextAfter( SourceLocation Loc, StringRef Str );
102 bool insertTextAfterToken( SourceLocation Loc, StringRef Str );
103 bool insertTextBefore( SourceLocation Loc, StringRef Str );
104 bool removeText( SourceLocation Start, unsigned Length, RewriteOptions opts = RewriteOptions());
105 // CharSourceRange not supported, unless really needed, as it needs handling for RemoveWholeStatement.
106 //bool removeText( CharSourceRange range, RewriteOptions opts = RewriteOptions());
107 bool removeText( SourceRange range, RewriteOptions opts = RewriteOptions());
108 bool replaceText( SourceLocation Start, unsigned OrigLength, StringRef NewStr );
109 bool replaceText( SourceRange range, StringRef NewStr );
110 bool replaceText( SourceRange range, SourceRange replacementRange );
111 Rewriter& rewriter;
112 private:
113 template< typename T > friend class Plugin::Registration;
114 template< typename T > static Plugin* createHelper( CompilerInstance& compiler, Rewriter& rewriter );
115 enum { isRewriter = true };
116 bool reportEditFailure( SourceLocation loc );
117 bool adjustForWholeStatement( SourceRange* range );
118 set< SourceLocation > removals;
122 Plugin registration helper.
124 If you create a new helper class, create also an instance of this class to automatically register it.
125 The passed argument is name of the plugin, used for explicitly invoking rewriter plugins
126 (it is ignored for non-rewriter plugins).
128 @code
129 static Plugin::Registration< NameOfClass > X( "nameofclass" );
130 @endcode
132 template< typename T >
133 class Plugin::Registration
135 public:
136 Registration( const char* optionName );
139 class RegistrationCreate
141 public:
142 template< typename T, bool > static T* create( CompilerInstance& compiler, Rewriter& rewriter );
145 /////
147 inline
148 Plugin::~Plugin()
152 inline
153 bool Plugin::ignoreLocation( const Decl* decl )
155 return ignoreLocation( decl->getLocation());
158 inline
159 bool Plugin::ignoreLocation( const Stmt* stmt )
161 return ignoreLocation( stmt->getLocStart());
164 template< typename T >
165 Plugin* Plugin::createHelper( CompilerInstance& compiler, Rewriter& )
167 return new T( compiler );
170 template< typename T >
171 Plugin* RewritePlugin::createHelper( CompilerInstance& compiler, Rewriter& rewriter )
173 return new T( compiler, rewriter );
176 template< typename T >
177 inline
178 Plugin::Registration< T >::Registration( const char* optionName )
180 registerPlugin( &T::template createHelper< T >, optionName, T::isRewriter );
183 } // namespace
185 #endif // COMPILEPLUGIN_H