blob: 13092a34bff102b28013db707003cd0cdbd31f45 [file] [log] [blame]
Benjamin Kramer6b236262016-04-20 12:43:43 +00001//===-- 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
Haojian Wu11e9bd22016-05-31 09:31:51 +000013#include "IncludeFixerContext.h"
Benjamin Kramera3d82332016-05-13 09:27:54 +000014#include "SymbolIndexManager.h"
Haojian Wu11e9bd22016-05-31 09:31:51 +000015#include "clang/Format/Format.h"
Benjamin Kramercd2494e92016-11-17 15:16:05 +000016#include "clang/Sema/ExternalSemaSource.h"
Benjamin Kramer6b236262016-04-20 12:43:43 +000017#include "clang/Tooling/Core/Replacement.h"
18#include "clang/Tooling/Tooling.h"
Eugene Zelenkoa54a2122016-05-02 17:49:00 +000019#include <memory>
20#include <vector>
Benjamin Kramer6b236262016-04-20 12:43:43 +000021
22namespace clang {
Eugene Zelenkoa54a2122016-05-02 17:49:00 +000023
24class CompilerInvocation;
25class DiagnosticConsumer;
26class FileManager;
27class PCHContainerOperations;
28
Benjamin Kramer6b236262016-04-20 12:43:43 +000029namespace include_fixer {
30
31class IncludeFixerActionFactory : public clang::tooling::ToolAction {
32public:
Benjamin Kramera3d82332016-05-13 09:27:54 +000033 /// \param SymbolIndexMgr A source for matching symbols to header files.
Simon Pilgrim6cfad712016-08-09 10:02:11 +000034 /// \param Contexts The contexts for the symbols being queried.
Eric Liu702cfd12016-05-19 08:21:09 +000035 /// \param StyleName Fallback style for reformatting.
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000036 /// \param MinimizeIncludePaths whether inserted include paths are optimized.
Haojian Wu11e9bd22016-05-31 09:31:51 +000037 IncludeFixerActionFactory(SymbolIndexManager &SymbolIndexMgr,
Haojian Wuc99f7282016-08-09 08:26:19 +000038 std::vector<IncludeFixerContext> &Contexts,
39 StringRef StyleName,
Haojian Wu11e9bd22016-05-31 09:31:51 +000040 bool MinimizeIncludePaths = true);
Eric Liu702cfd12016-05-19 08:21:09 +000041
Eugene Zelenkoa54a2122016-05-02 17:49:00 +000042 ~IncludeFixerActionFactory() override;
Benjamin Kramer6b236262016-04-20 12:43:43 +000043
44 bool
David Blaikiee83806b2017-01-06 19:49:09 +000045 runInvocation(std::shared_ptr<clang::CompilerInvocation> Invocation,
Benjamin Kramer6b236262016-04-20 12:43:43 +000046 clang::FileManager *Files,
47 std::shared_ptr<clang::PCHContainerOperations> PCHContainerOps,
48 clang::DiagnosticConsumer *Diagnostics) override;
49
50private:
51 /// The client to use to find cross-references.
Benjamin Kramera3d82332016-05-13 09:27:54 +000052 SymbolIndexManager &SymbolIndexMgr;
Benjamin Kramer6b236262016-04-20 12:43:43 +000053
Haojian Wuc99f7282016-08-09 08:26:19 +000054 /// Multiple contexts for files being processed.
55 std::vector<IncludeFixerContext> &Contexts;
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000056
57 /// Whether inserted include paths should be optimized.
58 bool MinimizeIncludePaths;
Eric Liu702cfd12016-05-19 08:21:09 +000059
60 /// The fallback format style for formatting after insertion if no
61 /// clang-format config file was found.
62 std::string FallbackStyle;
Benjamin Kramer6b236262016-04-20 12:43:43 +000063};
64
Haojian Wu62aee522016-07-21 13:47:09 +000065/// 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.
Haojian Wu11e9bd22016-05-31 09:31:51 +000068///
69/// \param Code The source code.
Haojian Wu62aee522016-07-21 13:47:09 +000070/// \param Context The context which contains all information for creating
71/// include-fixer replacements.
Haojian Wu11e9bd22016-05-31 09:31:51 +000072/// \param Style clang-format style being used.
Haojian Wu62aee522016-07-21 13:47:09 +000073/// \param AddQualifiers Whether we should add qualifiers to all instances of
74/// an unidentified symbol.
Haojian Wu11e9bd22016-05-31 09:31:51 +000075///
Haojian Wu62aee522016-07-21 13:47:09 +000076/// \return Formatted replacements for inserting, sorting headers and adding
77/// qualifiers on success; otherwise, an llvm::Error carrying llvm::StringError
78/// is returned.
79llvm::Expected<tooling::Replacements> createIncludeFixerReplacements(
Haojian Wuc99f7282016-08-09 08:26:19 +000080 StringRef Code, const IncludeFixerContext &Context,
Haojian Wu62aee522016-07-21 13:47:09 +000081 const format::FormatStyle &Style = format::getLLVMStyle(),
82 bool AddQualifiers = true);
Haojian Wu11e9bd22016-05-31 09:31:51 +000083
Benjamin Kramercd2494e92016-11-17 15:16:05 +000084/// Handles callbacks from sema, does the include lookup and turns it into an
85/// IncludeFixerContext.
86class IncludeFixerSemaSource : public clang::ExternalSemaSource {
87public:
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.
Benjamin Kramer87eb9662016-11-21 15:28:50 +0000118 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 }
Benjamin Kramercd2494e92016-11-17 15:16:05 +0000127
128private:
129 /// Query the database for a given identifier.
Benjamin Kramer87eb9662016-11-21 15:28:50 +0000130 std::vector<find_all_symbols::SymbolInfo>
131 query(StringRef Query, StringRef ScopedQualifiers, tooling::Range Range);
Benjamin Kramercd2494e92016-11-17 15:16:05 +0000132
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};
Benjamin Kramer6b236262016-04-20 12:43:43 +0000155} // namespace include_fixer
156} // namespace clang
157
158#endif // LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXER_H