clang-tools 10.0.0

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