2016-04-20 20:43:43 +08:00
|
|
|
//===-- IncludeFixer.h - Include inserter -----------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-20 20:43:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXER_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXER_H
|
|
|
|
|
2016-05-31 17:31:51 +08:00
|
|
|
#include "IncludeFixerContext.h"
|
2016-05-13 17:27:54 +08:00
|
|
|
#include "SymbolIndexManager.h"
|
2016-05-31 17:31:51 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2016-11-17 23:16:05 +08:00
|
|
|
#include "clang/Sema/ExternalSemaSource.h"
|
2016-04-20 20:43:43 +08:00
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
2016-05-03 01:49:00 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2016-04-20 20:43:43 +08:00
|
|
|
|
|
|
|
namespace clang {
|
2016-05-03 01:49:00 +08:00
|
|
|
|
|
|
|
class CompilerInvocation;
|
|
|
|
class DiagnosticConsumer;
|
|
|
|
class FileManager;
|
|
|
|
class PCHContainerOperations;
|
|
|
|
|
2016-04-20 20:43:43 +08:00
|
|
|
namespace include_fixer {
|
|
|
|
|
|
|
|
class IncludeFixerActionFactory : public clang::tooling::ToolAction {
|
|
|
|
public:
|
2016-05-13 17:27:54 +08:00
|
|
|
/// \param SymbolIndexMgr A source for matching symbols to header files.
|
2016-08-09 18:02:11 +08:00
|
|
|
/// \param Contexts The contexts for the symbols being queried.
|
2016-05-19 16:21:09 +08:00
|
|
|
/// \param StyleName Fallback style for reformatting.
|
2016-04-28 19:21:29 +08:00
|
|
|
/// \param MinimizeIncludePaths whether inserted include paths are optimized.
|
2016-05-31 17:31:51 +08:00
|
|
|
IncludeFixerActionFactory(SymbolIndexManager &SymbolIndexMgr,
|
2016-08-09 16:26:19 +08:00
|
|
|
std::vector<IncludeFixerContext> &Contexts,
|
|
|
|
StringRef StyleName,
|
2016-05-31 17:31:51 +08:00
|
|
|
bool MinimizeIncludePaths = true);
|
2016-05-19 16:21:09 +08:00
|
|
|
|
2016-05-03 01:49:00 +08:00
|
|
|
~IncludeFixerActionFactory() override;
|
2016-04-20 20:43:43 +08:00
|
|
|
|
|
|
|
bool
|
2017-01-07 03:49:09 +08:00
|
|
|
runInvocation(std::shared_ptr<clang::CompilerInvocation> Invocation,
|
2016-04-20 20:43:43 +08:00
|
|
|
clang::FileManager *Files,
|
|
|
|
std::shared_ptr<clang::PCHContainerOperations> PCHContainerOps,
|
|
|
|
clang::DiagnosticConsumer *Diagnostics) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// The client to use to find cross-references.
|
2016-05-13 17:27:54 +08:00
|
|
|
SymbolIndexManager &SymbolIndexMgr;
|
2016-04-20 20:43:43 +08:00
|
|
|
|
2016-08-09 16:26:19 +08:00
|
|
|
/// Multiple contexts for files being processed.
|
|
|
|
std::vector<IncludeFixerContext> &Contexts;
|
2016-04-28 19:21:29 +08:00
|
|
|
|
|
|
|
/// Whether inserted include paths should be optimized.
|
|
|
|
bool MinimizeIncludePaths;
|
2016-05-19 16:21:09 +08:00
|
|
|
|
|
|
|
/// The fallback format style for formatting after insertion if no
|
|
|
|
/// clang-format config file was found.
|
|
|
|
std::string FallbackStyle;
|
2016-04-20 20:43:43 +08:00
|
|
|
};
|
|
|
|
|
2016-07-21 21:47:09 +08:00
|
|
|
/// Create replacements, which are generated by clang-format, for the
|
|
|
|
/// missing header and mising qualifiers insertions. The function uses the
|
|
|
|
/// first header for insertion.
|
2016-05-31 17:31:51 +08:00
|
|
|
///
|
|
|
|
/// \param Code The source code.
|
2016-07-21 21:47:09 +08:00
|
|
|
/// \param Context The context which contains all information for creating
|
2019-03-25 22:09:10 +08:00
|
|
|
/// clang-include-fixer replacements.
|
2016-05-31 17:31:51 +08:00
|
|
|
/// \param Style clang-format style being used.
|
2016-07-21 21:47:09 +08:00
|
|
|
/// \param AddQualifiers Whether we should add qualifiers to all instances of
|
|
|
|
/// an unidentified symbol.
|
2016-05-31 17:31:51 +08:00
|
|
|
///
|
2016-07-21 21:47:09 +08:00
|
|
|
/// \return Formatted replacements for inserting, sorting headers and adding
|
|
|
|
/// qualifiers on success; otherwise, an llvm::Error carrying llvm::StringError
|
|
|
|
/// is returned.
|
|
|
|
llvm::Expected<tooling::Replacements> createIncludeFixerReplacements(
|
2016-08-09 16:26:19 +08:00
|
|
|
StringRef Code, const IncludeFixerContext &Context,
|
2016-07-21 21:47:09 +08:00
|
|
|
const format::FormatStyle &Style = format::getLLVMStyle(),
|
|
|
|
bool AddQualifiers = true);
|
2016-05-31 17:31:51 +08:00
|
|
|
|
2016-11-17 23:16:05 +08:00
|
|
|
/// Handles callbacks from sema, does the include lookup and turns it into an
|
|
|
|
/// IncludeFixerContext.
|
|
|
|
class IncludeFixerSemaSource : public clang::ExternalSemaSource {
|
|
|
|
public:
|
|
|
|
explicit IncludeFixerSemaSource(SymbolIndexManager &SymbolIndexMgr,
|
|
|
|
bool MinimizeIncludePaths,
|
|
|
|
bool GenerateDiagnostics)
|
|
|
|
: SymbolIndexMgr(SymbolIndexMgr),
|
|
|
|
MinimizeIncludePaths(MinimizeIncludePaths),
|
|
|
|
GenerateDiagnostics(GenerateDiagnostics) {}
|
|
|
|
|
|
|
|
void setCompilerInstance(CompilerInstance *CI) { this->CI = CI; }
|
2020-01-29 03:23:46 +08:00
|
|
|
void setFilePath(StringRef FilePath) {
|
|
|
|
this->FilePath = std::string(FilePath);
|
|
|
|
}
|
2016-11-17 23:16:05 +08:00
|
|
|
|
|
|
|
/// Callback for incomplete types. If we encounter a forward declaration we
|
|
|
|
/// have the fully qualified name ready. Just query that.
|
|
|
|
bool MaybeDiagnoseMissingCompleteType(clang::SourceLocation Loc,
|
|
|
|
clang::QualType T) override;
|
|
|
|
|
|
|
|
/// Callback for unknown identifiers. Try to piece together as much
|
|
|
|
/// qualification as we can get and do a query.
|
|
|
|
clang::TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo,
|
|
|
|
int LookupKind, Scope *S, CXXScopeSpec *SS,
|
|
|
|
CorrectionCandidateCallback &CCC,
|
|
|
|
DeclContext *MemberContext,
|
|
|
|
bool EnteringContext,
|
|
|
|
const ObjCObjectPointerType *OPT) override;
|
|
|
|
|
|
|
|
/// Get the minimal include for a given path.
|
|
|
|
std::string minimizeInclude(StringRef Include,
|
|
|
|
const clang::SourceManager &SourceManager,
|
|
|
|
clang::HeaderSearch &HeaderSearch) const;
|
|
|
|
|
|
|
|
/// Get the include fixer context for the queried symbol.
|
2016-11-21 23:28:50 +08:00
|
|
|
IncludeFixerContext getIncludeFixerContext(
|
|
|
|
const clang::SourceManager &SourceManager,
|
|
|
|
clang::HeaderSearch &HeaderSearch,
|
|
|
|
ArrayRef<find_all_symbols::SymbolInfo> MatchedSymbols) const;
|
|
|
|
|
|
|
|
/// Get the global matched symbols.
|
|
|
|
ArrayRef<find_all_symbols::SymbolInfo> getMatchedSymbols() const {
|
|
|
|
return MatchedSymbols;
|
|
|
|
}
|
2016-11-17 23:16:05 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Query the database for a given identifier.
|
2016-11-21 23:28:50 +08:00
|
|
|
std::vector<find_all_symbols::SymbolInfo>
|
|
|
|
query(StringRef Query, StringRef ScopedQualifiers, tooling::Range Range);
|
2016-11-17 23:16:05 +08:00
|
|
|
|
|
|
|
CompilerInstance *CI;
|
|
|
|
|
|
|
|
/// The client to use to find cross-references.
|
|
|
|
SymbolIndexManager &SymbolIndexMgr;
|
|
|
|
|
|
|
|
/// The information of the symbols being queried.
|
|
|
|
std::vector<IncludeFixerContext::QuerySymbolInfo> QuerySymbolInfos;
|
|
|
|
|
|
|
|
/// All symbol candidates which match QuerySymbol. We only include the first
|
|
|
|
/// discovered identifier to avoid getting caught in results from error
|
|
|
|
/// recovery.
|
|
|
|
std::vector<find_all_symbols::SymbolInfo> MatchedSymbols;
|
|
|
|
|
|
|
|
/// The file path to the file being processed.
|
|
|
|
std::string FilePath;
|
|
|
|
|
|
|
|
/// Whether we should use the smallest possible include path.
|
|
|
|
bool MinimizeIncludePaths = true;
|
|
|
|
|
|
|
|
/// Whether we should generate diagnostics with fixits for missing symbols.
|
|
|
|
bool GenerateDiagnostics = false;
|
|
|
|
};
|
2016-04-20 20:43:43 +08:00
|
|
|
} // namespace include_fixer
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXER_H
|