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.
15 #include <config_clang.h>
17 #include <clang/AST/ASTContext.h>
18 #include <clang/AST/RecursiveASTVisitor.h>
19 #include <clang/Basic/FileManager.h>
20 #include <clang/Basic/SourceManager.h>
21 #include <clang/Frontend/CompilerInstance.h>
22 #include <clang/Lex/Preprocessor.h>
23 #include <unordered_map>
25 #if __clang_major__ < 3 || __clang_major__ == 3 && __clang_minor__ < 2
26 #include <clang/Rewrite/Rewriter.h>
28 #include <clang/Rewrite/Core/Rewriter.h>
31 using namespace clang
;
41 Base class for plugins.
43 If you want to create a non-rewriter action, inherit from this class. Remember to also
44 use Plugin::Registration.
49 struct InstantiationData
52 PluginHandler
& handler
;
53 CompilerInstance
& compiler
;
56 explicit Plugin( const InstantiationData
& data
);
58 virtual void run() = 0;
59 template< typename T
> class Registration
;
60 enum { isPPCallback
= false };
61 // Returns location right after the end of the token that starts at the given location.
62 SourceLocation
locationAfterToken( SourceLocation location
);
64 DiagnosticBuilder
report( DiagnosticsEngine::Level level
, StringRef message
, SourceLocation loc
= SourceLocation()) const;
65 bool ignoreLocation( SourceLocation loc
);
66 bool ignoreLocation( const Decl
* decl
);
67 bool ignoreLocation( const Stmt
* stmt
);
68 CompilerInstance
& compiler
;
69 PluginHandler
& handler
;
71 Returns the parent of the given AST node. Clang's internal AST representation doesn't provide this information,
72 it can only provide children, but getting the parent is often useful for inspecting a part of the AST.
74 const Stmt
* parentStmt( const Stmt
* stmt
);
75 Stmt
* parentStmt( Stmt
* stmt
);
77 Checks if the location is inside an UNO file, more specifically, if it forms part of the URE stable interface,
78 which is not allowed to be changed.
80 bool isInUnoIncludeFile(SourceLocation spellingLocation
) const;
82 static void registerPlugin( Plugin
* (*create
)( const InstantiationData
& ), const char* optionName
, bool isPPCallback
, bool byDefault
);
83 template< typename T
> static Plugin
* createHelper( const InstantiationData
& data
);
84 enum { isRewriter
= false };
86 static unordered_map
< const Stmt
*, const Stmt
* > parents
;
87 static void buildParents( CompilerInstance
& compiler
);
91 Base class for rewriter plugins.
93 Remember to also use Plugin::Registration.
99 explicit RewritePlugin( const InstantiationData
& data
);
103 // This enum allows passing just 'RemoveLineIfEmpty' to functions below.
104 // If the resulting line would be completely empty, it'll be removed.
105 RemoveLineIfEmpty
= 1 << 0,
106 // Use this to remove the declaration/statement as a whole, i.e. all whitespace before the statement
107 // and the trailing semicolor (is not part of the AST element range itself).
108 // The trailing semicolon must be present.
109 RemoveWholeStatement
= 1 << 1,
110 // Removes also all whitespace preceding and following the expression (completely, so that
111 // the preceding and following tokens would be right next to each other, follow with insertText( " " )
112 // if this is not wanted). Despite the name, indentation whitespace is not removed.
113 RemoveAllWhitespace
= 1 << 2
115 struct RewriteOptions
116 : public Rewriter::RewriteOptions
119 RewriteOptions( RewriteOption option
);
122 // syntactic sugar to be able to write 'RemoveLineIfEmpty | RemoveWholeStatement'
123 friend RewriteOption
operator|( RewriteOption option1
, RewriteOption option2
);
124 // These following insert/remove/replaceText functions map to functions
125 // in clang::Rewriter, with these differences:
126 // - they (more intuitively) return false on failure rather than true
127 // - they report a warning when the change cannot be done
128 // - There are more options for easier removal of surroundings of a statement/expression.
129 bool insertText( SourceLocation Loc
, StringRef Str
,
130 bool InsertAfter
= true, bool indentNewLines
= false );
131 bool insertTextAfter( SourceLocation Loc
, StringRef Str
);
132 bool insertTextAfterToken( SourceLocation Loc
, StringRef Str
);
133 bool insertTextBefore( SourceLocation Loc
, StringRef Str
);
134 bool removeText( SourceLocation Start
, unsigned Length
, RewriteOptions opts
= RewriteOptions());
135 bool removeText( CharSourceRange range
, RewriteOptions opts
= RewriteOptions());
136 bool removeText( SourceRange range
, RewriteOptions opts
= RewriteOptions());
137 bool replaceText( SourceLocation Start
, unsigned OrigLength
, StringRef NewStr
);
138 bool replaceText( SourceRange range
, StringRef NewStr
);
139 bool replaceText( SourceRange range
, SourceRange replacementRange
);
142 template< typename T
> friend class Plugin::Registration
;
143 enum { isRewriter
= true };
144 bool reportEditFailure( SourceLocation loc
);
145 bool adjustRangeForOptions( CharSourceRange
* range
, RewriteOptions options
);
149 Plugin registration helper.
151 If you create a new helper class, create also an instance of this class to automatically register it.
152 The passed argument is name of the plugin, used for explicitly invoking rewriter plugins
153 (it is ignored for non-rewriter plugins).
156 static Plugin::Registration< NameOfClass > X( "nameofclass" );
159 template< typename T
>
160 class Plugin::Registration
163 Registration( const char* optionName
, bool byDefault
= !T::isRewriter
);
166 class RegistrationCreate
169 template< typename T
, bool > static T
* create( const Plugin::InstantiationData
& data
);
178 bool Plugin::ignoreLocation( const Decl
* decl
)
180 return ignoreLocation( decl
->getLocation());
184 bool Plugin::ignoreLocation( const Stmt
* stmt
)
186 // Invalid location can happen at least for ImplicitCastExpr of
187 // ImplicitParam 'self' in Objective C method declarations:
188 return stmt
->getLocStart().isValid() && ignoreLocation( stmt
->getLocStart());
191 template< typename T
>
192 Plugin
* Plugin::createHelper( const InstantiationData
& data
)
194 return new T( data
);
197 template< typename T
>
199 Plugin::Registration
< T
>::Registration( const char* optionName
, bool byDefault
)
201 registerPlugin( &T::template createHelper
< T
>, optionName
, T::isPPCallback
, byDefault
);
205 RewritePlugin::RewriteOptions::RewriteOptions()
211 RewritePlugin::RewriteOptions::RewriteOptions( RewriteOption option
)
214 // Note that 'flags' stores also RemoveLineIfEmpty, it must be kept in sync with the base class.
215 if( flags
& RewritePlugin::RemoveLineIfEmpty
)
216 this->RemoveLineIfEmpty
= true;
220 RewritePlugin::RewriteOption
operator|( RewritePlugin::RewriteOption option1
, RewritePlugin::RewriteOption option2
)
222 return static_cast< RewritePlugin::RewriteOption
>( int( option1
) | int( option2
));
227 #endif // COMPILEPLUGIN_H
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */