blob: 0d6bdb75b5ac74e41fdaa268da34cd905ec2593d [file] [log] [blame]
Benjamin Kramercd2494e92016-11-17 15:16:05 +00001//===- IncludeFixerPlugin.cpp - clang-include-fixer as a clang plugin -----===//
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#include "../IncludeFixer.h"
11#include "../YamlSymbolIndex.h"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Frontend/FrontendPluginRegistry.h"
14#include "clang/Parse/ParseAST.h"
15#include "clang/Sema/Sema.h"
16#include "llvm/Support/Path.h"
17
18namespace clang {
19namespace include_fixer {
20
21/// The core include fixer plugin action. This just provides the AST consumer
22/// and command line flag parsing for using include fixer as a clang plugin.
23class ClangIncludeFixerPluginAction : public PluginASTAction {
24 /// ASTConsumer to keep the symbol index alive. We don't really need an
25 /// ASTConsumer for this plugin (everything is funneled on the side through
26 /// Sema) but we have to keep the symbol index alive until sema is done.
27 struct ASTConsumerManagerWrapper : public ASTConsumer {
28 ASTConsumerManagerWrapper(std::shared_ptr<SymbolIndexManager> SIM)
29 : SymbolIndexMgr(std::move(SIM)) {}
30 std::shared_ptr<SymbolIndexManager> SymbolIndexMgr;
31 };
32
33public:
34 explicit ClangIncludeFixerPluginAction()
35 : SymbolIndexMgr(std::make_shared<SymbolIndexManager>()),
36 SemaSource(new IncludeFixerSemaSource(*SymbolIndexMgr,
37 /*MinimizeIncludePaths=*/true,
38 /*GenerateDiagnostics=*/true)) {}
39
40 std::unique_ptr<clang::ASTConsumer>
41 CreateASTConsumer(clang::CompilerInstance &CI, StringRef InFile) override {
42 CI.setExternalSemaSource(SemaSource);
43 SemaSource->setFilePath(InFile);
44 SemaSource->setCompilerInstance(&CI);
45 return llvm::make_unique<ASTConsumerManagerWrapper>(SymbolIndexMgr);
46 }
47
48 void ExecuteAction() override {} // Do nothing.
49
50 bool ParseArgs(const CompilerInstance &CI,
51 const std::vector<std::string> &Args) override {
52 StringRef DB = "yaml";
53 StringRef Input;
54
55 // Parse the extra command line args.
56 // FIXME: This is very limited at the moment.
57 for (StringRef Arg : Args) {
58 if (Arg.startswith("-db="))
59 DB = Arg.substr(strlen("-db="));
60 else if (Arg.startswith("-input="))
61 Input = Arg.substr(strlen("-input="));
62 }
63
Benjamin Kramerbdb21712017-01-09 15:18:28 +000064 std::string InputFile = CI.getFrontendOpts().Inputs[0].getFile();
65 auto CreateYamlIdx = [=]() -> std::unique_ptr<include_fixer::SymbolIndex> {
66 llvm::ErrorOr<std::unique_ptr<include_fixer::YamlSymbolIndex>> SymbolIdx(
67 nullptr);
68 if (DB == "yaml") {
69 if (!Input.empty()) {
70 SymbolIdx = include_fixer::YamlSymbolIndex::createFromFile(Input);
71 } else {
72 // If we don't have any input file, look in the directory of the first
73 // file and its parents.
74 SmallString<128> AbsolutePath(tooling::getAbsolutePath(InputFile));
75 StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
76 SymbolIdx = include_fixer::YamlSymbolIndex::createFromDirectory(
77 Directory, "find_all_symbols_db.yaml");
78 }
Benjamin Kramercd2494e92016-11-17 15:16:05 +000079 }
Benjamin Kramerbdb21712017-01-09 15:18:28 +000080 return std::move(*SymbolIdx);
81 };
82
83 SymbolIndexMgr->addSymbolIndex(std::move(CreateYamlIdx));
Benjamin Kramercd2494e92016-11-17 15:16:05 +000084 return true;
85 }
86
87private:
88 std::shared_ptr<SymbolIndexManager> SymbolIndexMgr;
89 IntrusiveRefCntPtr<IncludeFixerSemaSource> SemaSource;
90};
91} // namespace include_fixer
92} // namespace clang
93
94// This anchor is used to force the linker to link in the generated object file
95// and thus register the include fixer plugin.
96volatile int ClangIncludeFixerPluginAnchorSource = 0;
97
98static clang::FrontendPluginRegistry::Add<
99 clang::include_fixer::ClangIncludeFixerPluginAction>
100 X("clang-include-fixer", "clang-include-fixer");