Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 1 | //===-- ClangIncludeFixer.cpp - Standalone include fixer ------------------===// |
| 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 | |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 10 | #include "InMemorySymbolIndex.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 11 | #include "IncludeFixer.h" |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 12 | #include "SymbolIndexManager.h" |
| 13 | #include "YamlSymbolIndex.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 15 | #include "clang/Rewrite/Core/Rewriter.h" |
| 16 | #include "clang/Tooling/CommonOptionsParser.h" |
| 17 | #include "clang/Tooling/Tooling.h" |
| 18 | #include "llvm/Support/CommandLine.h" |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 19 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 20 | using namespace clang; |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 22 | |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | cl::OptionCategory IncludeFixerCategory("Tool options"); |
| 25 | |
| 26 | enum DatabaseFormatTy { |
Simon Pilgrim | b9f2558 | 2016-04-27 20:43:32 +0000 | [diff] [blame] | 27 | fixed, ///< Hard-coded mapping. |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 28 | yaml, ///< Yaml database created by find-all-symbols. |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | cl::opt<DatabaseFormatTy> DatabaseFormat( |
| 32 | "db", cl::desc("Specify input format"), |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 33 | cl::values(clEnumVal(fixed, "Hard-coded mapping"), |
| 34 | clEnumVal(yaml, "Yaml database created by find-all-symbols"), |
| 35 | clEnumValEnd), |
Benjamin Kramer | 8fd85a5 | 2016-05-10 11:35:47 +0000 | [diff] [blame] | 36 | cl::init(yaml), cl::cat(IncludeFixerCategory)); |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 37 | |
| 38 | cl::opt<std::string> Input("input", |
| 39 | cl::desc("String to initialize the database"), |
| 40 | cl::cat(IncludeFixerCategory)); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 41 | |
| 42 | cl::opt<bool> |
| 43 | MinimizeIncludePaths("minimize-paths", |
| 44 | cl::desc("Whether to minimize added include paths"), |
| 45 | cl::init(true), cl::cat(IncludeFixerCategory)); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 46 | |
Benjamin Kramer | 5471829 | 2016-05-10 08:36:56 +0000 | [diff] [blame] | 47 | cl::opt<bool> Quiet("q", cl::desc("Reduce terminal output"), cl::init(false), |
| 48 | cl::cat(IncludeFixerCategory)); |
| 49 | |
Haojian Wu | a315dcb | 2016-05-03 08:38:35 +0000 | [diff] [blame] | 50 | int includeFixerMain(int argc, const char **argv) { |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 51 | tooling::CommonOptionsParser options(argc, argv, IncludeFixerCategory); |
| 52 | tooling::ClangTool tool(options.getCompilations(), |
| 53 | options.getSourcePathList()); |
| 54 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 55 | // Set up data source. |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 56 | auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>(); |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 57 | switch (DatabaseFormat) { |
| 58 | case fixed: { |
| 59 | // Parse input and fill the database with it. |
| 60 | // <symbol>=<header><, header...> |
| 61 | // Multiple symbols can be given, separated by semicolons. |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 62 | std::map<std::string, std::vector<std::string>> SymbolsMap; |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 63 | SmallVector<StringRef, 4> SemicolonSplits; |
| 64 | StringRef(Input).split(SemicolonSplits, ";"); |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 65 | std::vector<find_all_symbols::SymbolInfo> Symbols; |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 66 | for (StringRef Pair : SemicolonSplits) { |
| 67 | auto Split = Pair.split('='); |
| 68 | std::vector<std::string> Headers; |
| 69 | SmallVector<StringRef, 4> CommaSplits; |
| 70 | Split.second.split(CommaSplits, ","); |
| 71 | for (StringRef Header : CommaSplits) |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 72 | Symbols.push_back(find_all_symbols::SymbolInfo( |
| 73 | Split.first.trim(), |
| 74 | find_all_symbols::SymbolInfo::SymbolKind::Unknown, Header.trim(), 1, |
| 75 | {})); |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 76 | } |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 77 | SymbolIndexMgr->addSymbolIndex( |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 78 | llvm::make_unique<include_fixer::InMemorySymbolIndex>(Symbols)); |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 79 | break; |
| 80 | } |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 81 | case yaml: { |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 82 | llvm::ErrorOr<std::unique_ptr<include_fixer::YamlSymbolIndex>> DB(nullptr); |
Benjamin Kramer | b6aed5f4 | 2016-05-09 14:14:55 +0000 | [diff] [blame] | 83 | if (!Input.empty()) { |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 84 | DB = include_fixer::YamlSymbolIndex::createFromFile(Input); |
Benjamin Kramer | b6aed5f4 | 2016-05-09 14:14:55 +0000 | [diff] [blame] | 85 | } else { |
| 86 | // If we don't have any input file, look in the directory of the first |
| 87 | // file and its parents. |
| 88 | SmallString<128> AbsolutePath( |
| 89 | tooling::getAbsolutePath(options.getSourcePathList().front())); |
| 90 | StringRef Directory = llvm::sys::path::parent_path(AbsolutePath); |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 91 | DB = include_fixer::YamlSymbolIndex::createFromDirectory( |
Benjamin Kramer | b6aed5f4 | 2016-05-09 14:14:55 +0000 | [diff] [blame] | 92 | Directory, "find_all_symbols_db.yaml"); |
| 93 | } |
| 94 | |
| 95 | if (!DB) { |
| 96 | llvm::errs() << "Couldn't find YAML db: " << DB.getError().message() |
| 97 | << '\n'; |
| 98 | return 1; |
| 99 | } |
| 100 | |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 101 | SymbolIndexMgr->addSymbolIndex(std::move(*DB)); |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 102 | break; |
| 103 | } |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 104 | } |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 105 | |
| 106 | // Now run our tool. |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 107 | std::vector<tooling::Replacement> Replacements; |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 108 | include_fixer::IncludeFixerActionFactory Factory( |
| 109 | *SymbolIndexMgr, Replacements, MinimizeIncludePaths); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 110 | |
Benjamin Kramer | 6b5160a | 2016-05-18 13:32:38 +0000 | [diff] [blame] | 111 | if (tool.run(&Factory) != 0) { |
| 112 | llvm::errs() |
| 113 | << "Clang died with a fatal error! (incorrect include paths?)\n"; |
| 114 | return 1; |
| 115 | } |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 116 | |
Benjamin Kramer | 5471829 | 2016-05-10 08:36:56 +0000 | [diff] [blame] | 117 | if (!Quiet) |
| 118 | for (const tooling::Replacement &Replacement : Replacements) |
| 119 | llvm::errs() << "Added " << Replacement.getReplacementText(); |
| 120 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 121 | // Set up a new source manager for applying the resulting replacements. |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 122 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); |
| 123 | DiagnosticsEngine Diagnostics(new DiagnosticIDs, &*DiagOpts); |
| 124 | TextDiagnosticPrinter DiagnosticPrinter(outs(), &*DiagOpts); |
| 125 | SourceManager SM(Diagnostics, tool.getFiles()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 126 | Diagnostics.setClient(&DiagnosticPrinter, false); |
| 127 | |
| 128 | // Write replacements to disk. |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 129 | Rewriter Rewrites(SM, LangOptions()); |
| 130 | tooling::applyAllReplacements(Replacements, Rewrites); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 131 | return Rewrites.overwriteChangedFiles(); |
| 132 | } |
Haojian Wu | a315dcb | 2016-05-03 08:38:35 +0000 | [diff] [blame] | 133 | |
| 134 | } // namespace |
| 135 | |
| 136 | int main(int argc, const char **argv) { |
| 137 | return includeFixerMain(argc, argv); |
| 138 | } |