2 * This file is part of the LibreOffice project.
6 * This file is distributed under the University of Illinois Open Source
7 * License. See LICENSE.TXT for details.
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>
23 #if __clang_major__ < 3 || __clang_major__ == 3 && __clang_minor__ < 2
24 #include <clang/Rewrite/Rewriter.h>
26 #include <clang/Rewrite/Core/Rewriter.h>
29 using namespace clang
;
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.
45 explicit Plugin( CompilerInstance
& compiler
);
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());
53 bool ignoreLocation( SourceLocation loc
);
54 bool ignoreLocation( const Decl
* decl
);
55 bool ignoreLocation( const Stmt
* stmt
);
56 CompilerInstance
& compiler
;
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 };
64 Base class for rewriter plugins.
66 Remember to also use Plugin::Registration.
72 explicit RewritePlugin( CompilerInstance
& compiler
, Rewriter
& rewriter
);
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
; }
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
);
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).
129 static Plugin::Registration< NameOfClass > X( "nameofclass" );
132 template< typename T
>
133 class Plugin::Registration
136 Registration( const char* optionName
);
139 class RegistrationCreate
142 template< typename T
, bool > static T
* create( CompilerInstance
& compiler
, Rewriter
& rewriter
);
153 bool Plugin::ignoreLocation( const Decl
* decl
)
155 return ignoreLocation( decl
->getLocation());
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
>
178 Plugin::Registration
< T
>::Registration( const char* optionName
)
180 registerPlugin( &T::template createHelper
< T
>, optionName
, T::isRewriter
);
185 #endif // COMPILEPLUGIN_H