clang-tools 8.0.0

IncludeFixer.h
Go to the documentation of this file.
1 //===-- IncludeFixer.h - Include inserter -----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXER_H
11 #define LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXER_H
12
13 #include "IncludeFixerContext.h"
14 #include "SymbolIndexManager.h"
15 #include "clang/Format/Format.h"
16 #include "clang/Sema/ExternalSemaSource.h"
17 #include "clang/Tooling/Core/Replacement.h"
18 #include "clang/Tooling/Tooling.h"
19 #include <memory>
20 #include <vector>
21
22 namespace clang {
23
24 class CompilerInvocation;
25 class DiagnosticConsumer;
26 class FileManager;
27 class PCHContainerOperations;
28
29 namespace include_fixer {
30
31 class IncludeFixerActionFactory : public clang::tooling::ToolAction {
32 public:
33 /// \param SymbolIndexMgr A source for matching symbols to header files.
34 /// \param Contexts The contexts for the symbols being queried.
35 /// \param StyleName Fallback style for reformatting.
36 /// \param MinimizeIncludePaths whether inserted include paths are optimized.
37 IncludeFixerActionFactory(SymbolIndexManager &SymbolIndexMgr,
38 std::vector<IncludeFixerContext> &Contexts,
39 StringRef StyleName,
40 bool MinimizeIncludePaths = true);
41
42 ~IncludeFixerActionFactory() override;
43
44 bool
45 runInvocation(std::shared_ptr<clang::CompilerInvocation> Invocation,
46 clang::FileManager *Files,
47 std::shared_ptr<clang::PCHContainerOperations> PCHContainerOps,
48 clang::DiagnosticConsumer *Diagnostics) override;
49
50 private:
51 /// The client to use to find cross-references.
52 SymbolIndexManager &SymbolIndexMgr;
53
54 /// Multiple contexts for files being processed.
55 std::vector<IncludeFixerContext> &Contexts;
56
57 /// Whether inserted include paths should be optimized.
58 bool MinimizeIncludePaths;
59
60 /// The fallback format style for formatting after insertion if no
61 /// clang-format config file was found.
62 std::string FallbackStyle;
63 };
64
65 /// Create replacements, which are generated by clang-format, for the
66 /// missing header and mising qualifiers insertions. The function uses the
67 /// first header for insertion.
68 ///
69 /// \param Code The source code.
70 /// \param Context The context which contains all information for creating
71 /// include-fixer replacements.
72 /// \param Style clang-format style being used.
73 /// \param AddQualifiers Whether we should add qualifiers to all instances of
74 /// an unidentified symbol.
75 ///
76 /// \return Formatted replacements for inserting, sorting headers and adding
77 /// qualifiers on success; otherwise, an llvm::Error carrying llvm::StringError
78 /// is returned.
79 llvm::Expected<tooling::Replacements> createIncludeFixerReplacements(
80 StringRef Code, const IncludeFixerContext &Context,
81 const format::FormatStyle &Style = format::getLLVMStyle(),
82 bool AddQualifiers = true);
83
84 /// Handles callbacks from sema, does the include lookup and turns it into an
85 /// IncludeFixerContext.
86 class IncludeFixerSemaSource : public clang::ExternalSemaSource {
87 public:
88 explicit IncludeFixerSemaSource(SymbolIndexManager &SymbolIndexMgr,
89 bool MinimizeIncludePaths,
90 bool GenerateDiagnostics)
91 : SymbolIndexMgr(SymbolIndexMgr),
92 MinimizeIncludePaths(MinimizeIncludePaths),
93 GenerateDiagnostics(GenerateDiagnostics) {}
94
95 void setCompilerInstance(CompilerInstance *CI) { this->CI = CI; }
96 void setFilePath(StringRef FilePath) { this->FilePath = FilePath; }
97
98 /// Callback for incomplete types. If we encounter a forward declaration we
99 /// have the fully qualified name ready. Just query that.
100 bool MaybeDiagnoseMissingCompleteType(clang::SourceLocation Loc,
101 clang::QualType T) override;
102
103 /// Callback for unknown identifiers. Try to piece together as much
104 /// qualification as we can get and do a query.
105 clang::TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo,
106 int LookupKind, Scope *S, CXXScopeSpec *SS,
107 CorrectionCandidateCallback &CCC,
108 DeclContext *MemberContext,
109 bool EnteringContext,
110 const ObjCObjectPointerType *OPT) override;
111
112 /// Get the minimal include for a given path.
113 std::string minimizeInclude(StringRef Include,
114 const clang::SourceManager &SourceManager,
115 clang::HeaderSearch &HeaderSearch) const;
116
117 /// Get the include fixer context for the queried symbol.
118 IncludeFixerContext getIncludeFixerContext(
119 const clang::SourceManager &SourceManager,
120 clang::HeaderSearch &HeaderSearch,
121 ArrayRef<find_all_symbols::SymbolInfo> MatchedSymbols) const;
122
123 /// Get the global matched symbols.
124 ArrayRef<find_all_symbols::SymbolInfo> getMatchedSymbols() const {
125 return MatchedSymbols;
126 }
127
128 private:
129 /// Query the database for a given identifier.
130 std::vector<find_all_symbols::SymbolInfo>
131 query(StringRef Query, StringRef ScopedQualifiers, tooling::Range Range);
132
133 CompilerInstance *CI;
134
135 /// The client to use to find cross-references.
136 SymbolIndexManager &SymbolIndexMgr;
137
138 /// The information of the symbols being queried.
139 std::vector<IncludeFixerContext::QuerySymbolInfo> QuerySymbolInfos;
140
141 /// All symbol candidates which match QuerySymbol. We only include the first
142 /// discovered identifier to avoid getting caught in results from error
143 /// recovery.
144 std::vector<find_all_symbols::SymbolInfo> MatchedSymbols;
145
146 /// The file path to the file being processed.
147 std::string FilePath;
148
149 /// Whether we should use the smallest possible include path.
150 bool MinimizeIncludePaths = true;
151
152 /// Whether we should generate diagnostics with fixits for missing symbols.
153 bool GenerateDiagnostics = false;
154 };
155 } // namespace include_fixer
156 } // namespace clang
157
158 #endif // LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXER_H
DiagnosticConsumer
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::include_fixer::IncludeFixerSemaSource
Handles callbacks from sema, does the include lookup and turns it into an IncludeFixerContext.
Definition: IncludeFixer.h:86
ToolAction
clang::include_fixer::createIncludeFixerReplacements
llvm::Expected< tooling::Replacements > createIncludeFixerReplacements(StringRef Code, const IncludeFixerContext &Context, const clang::format::FormatStyle &Style, bool AddQualifiers)
Definition: IncludeFixer.cpp:403
clang::include_fixer::IncludeFixerContext
A context for a file being processed.
Definition: IncludeFixerContext.h:23
clang::include_fixer::IncludeFixerSemaSource::IncludeFixerSemaSource
IncludeFixerSemaSource(SymbolIndexManager &SymbolIndexMgr, bool MinimizeIncludePaths, bool GenerateDiagnostics)
Definition: IncludeFixer.h:88
clang::include_fixer::IncludeFixerActionFactory::runInvocation
bool runInvocation(std::shared_ptr< clang::CompilerInvocation > Invocation, clang::FileManager *Files, std::shared_ptr< clang::PCHContainerOperations > PCHContainerOps, clang::DiagnosticConsumer *Diagnostics) override
Definition: IncludeFixer.cpp:84
SymbolIndexManager.h
clang::include_fixer::IncludeFixerSemaSource::getMatchedSymbols
ArrayRef< find_all_symbols::SymbolInfo > getMatchedSymbols() const
Get the global matched symbols.
Definition: IncludeFixer.h:124
clang::include_fixer::SymbolIndexManager
This class provides an interface for finding the header files corresponding to an identifier in the s...
Definition: SymbolIndexManager.h:34
clang::include_fixer::IncludeFixerActionFactory::~IncludeFixerActionFactory
~IncludeFixerActionFactory() override
clang::include_fixer::IncludeFixerSemaSource::setFilePath
void setFilePath(StringRef FilePath)
Definition: IncludeFixer.h:96
clang::include_fixer::IncludeFixerActionFactory::IncludeFixerActionFactory
IncludeFixerActionFactory(SymbolIndexManager &SymbolIndexMgr, std::vector< IncludeFixerContext > &Contexts, StringRef StyleName, bool MinimizeIncludePaths=true)
Definition: IncludeFixer.cpp:75
IncludeFixerContext.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::include_fixer::IncludeFixerActionFactory
Definition: IncludeFixer.h:31
clang::include_fixer::IncludeFixerSemaSource::setCompilerInstance
void setCompilerInstance(CompilerInstance *CI)
Definition: IncludeFixer.h:95
FormatStyle
static cl::opt< std::string > FormatStyle("format-style", cl::desc(R"( Style for formatting code around applied fixes: - 'none' (default) turns off formatting - 'file' (literally 'file', not a placeholder) uses .clang-format file in the closest parent directory - '{ <json> }' specifies options inline, e.g. -format-style='{BasedOnStyle: llvm, IndentWidth: 8}' - 'llvm', 'google', 'webkit', 'mozilla' See clang-format documentation for the up-to-date information about formatting styles and options. This option overrides the 'FormatStyle` option in .clang-tidy file, if any. )"), cl::init("none"), cl::cat(ClangTidyCategory))