9 #include "../IncludeFixer.h"
10 #include "../YamlSymbolIndex.h"
11 #include "clang/Frontend/CompilerInstance.h"
12 #include "clang/Frontend/FrontendPluginRegistry.h"
13 #include "clang/Parse/ParseAST.h"
14 #include "clang/Sema/Sema.h"
15 #include "llvm/Support/Path.h"
18 namespace include_fixer {
26 struct ASTConsumerManagerWrapper :
public ASTConsumer {
27 ASTConsumerManagerWrapper(std::shared_ptr<SymbolIndexManager> SIM)
28 : SymbolIndexMgr(std::move(SIM)) {}
29 std::shared_ptr<SymbolIndexManager> SymbolIndexMgr;
39 std::unique_ptr<clang::ASTConsumer>
41 CI.setExternalSemaSource(SemaSource);
42 SemaSource->setFilePath(InFile);
43 SemaSource->setCompilerInstance(&
CI);
44 return std::make_unique<ASTConsumerManagerWrapper>(SymbolIndexMgr);
50 const std::vector<std::string> &Args)
override {
51 StringRef DB =
"yaml";
56 for (StringRef Arg : Args) {
57 if (Arg.startswith(
"-db="))
58 DB = Arg.substr(strlen(
"-db="));
59 else if (Arg.startswith(
"-input="))
60 Input = Arg.substr(strlen(
"-input="));
63 std::string InputFile =
64 std::string(
CI.getFrontendOpts().Inputs[0].getFile());
65 auto CreateYamlIdx = [=]() -> std::unique_ptr<include_fixer::SymbolIndex> {
66 llvm::ErrorOr<std::unique_ptr<include_fixer::YamlSymbolIndex>> SymbolIdx(
74 SmallString<128> AbsolutePath(tooling::getAbsolutePath(InputFile));
75 StringRef
Directory = llvm::sys::path::parent_path(AbsolutePath);
80 return std::move(*SymbolIdx);
83 SymbolIndexMgr->addSymbolIndex(std::move(CreateYamlIdx));
88 std::shared_ptr<SymbolIndexManager> SymbolIndexMgr;
89 IntrusiveRefCntPtr<IncludeFixerSemaSource> SemaSource;
98 static clang::FrontendPluginRegistry::Add<
100 X(
"clang-include-fixer",
"clang-include-fixer");