lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / compilerplugins / clang / plugin.hxx
blob867396cdcb436603a13007ce4f0c362f647594ea
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 #ifndef PLUGIN_H
13 #define PLUGIN_H
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>
27 #else
28 #include <clang/Rewrite/Core/Rewriter.h>
29 #endif
31 using namespace clang;
32 using namespace llvm;
33 using namespace std;
35 namespace loplugin
38 class PluginHandler;
40 /**
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.
46 class Plugin
48 public:
49 struct InstantiationData
51 const char* name;
52 PluginHandler& handler;
53 CompilerInstance& compiler;
54 Rewriter* rewriter;
56 explicit Plugin( const InstantiationData& data );
57 virtual ~Plugin();
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 );
63 protected:
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;
70 /**
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 );
76 /**
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;
81 private:
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 };
85 const char* name;
86 static unordered_map< const Stmt*, const Stmt* > parents;
87 static void buildParents( CompilerInstance& compiler );
90 /**
91 Base class for rewriter plugins.
93 Remember to also use Plugin::Registration.
95 class RewritePlugin
96 : public Plugin
98 public:
99 explicit RewritePlugin( const InstantiationData& data );
100 protected:
101 enum RewriteOption
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
118 RewriteOptions();
119 RewriteOptions( RewriteOption option );
120 const int flags;
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 );
140 Rewriter* rewriter;
141 private:
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).
155 @code
156 static Plugin::Registration< NameOfClass > X( "nameofclass" );
157 @endcode
159 template< typename T >
160 class Plugin::Registration
162 public:
163 Registration( const char* optionName, bool byDefault = !T::isRewriter );
166 class RegistrationCreate
168 public:
169 template< typename T, bool > static T* create( const Plugin::InstantiationData& data );
172 inline
173 Plugin::~Plugin()
177 inline
178 bool Plugin::ignoreLocation( const Decl* decl )
180 return ignoreLocation( decl->getLocation());
183 inline
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 >
198 inline
199 Plugin::Registration< T >::Registration( const char* optionName, bool byDefault )
201 registerPlugin( &T::template createHelper< T >, optionName, T::isPPCallback, byDefault );
204 inline
205 RewritePlugin::RewriteOptions::RewriteOptions()
206 : flags( 0 )
210 inline
211 RewritePlugin::RewriteOptions::RewriteOptions( RewriteOption option )
212 : flags( 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;
219 inline
220 RewritePlugin::RewriteOption operator|( RewritePlugin::RewriteOption option1, RewritePlugin::RewriteOption option2 )
222 return static_cast< RewritePlugin::RewriteOption >( int( option1 ) | int( option2 ));
225 } // namespace
227 #endif // COMPILEPLUGIN_H
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */