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 | |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame^] | 50 | cl::opt<bool> |
| 51 | STDINMode("stdin", |
| 52 | cl::desc("Override source file's content (in the overlaying\n" |
| 53 | "virtual file system) with input from <stdin> and run\n" |
| 54 | "the tool on the new content with the compilation\n" |
| 55 | "options of the source file. This mode is currently\n" |
| 56 | "used for editor integration."), |
| 57 | cl::init(false), cl::cat(IncludeFixerCategory)); |
| 58 | |
Haojian Wu | a315dcb | 2016-05-03 08:38:35 +0000 | [diff] [blame] | 59 | int includeFixerMain(int argc, const char **argv) { |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 60 | tooling::CommonOptionsParser options(argc, argv, IncludeFixerCategory); |
| 61 | tooling::ClangTool tool(options.getCompilations(), |
| 62 | options.getSourcePathList()); |
| 63 | |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame^] | 64 | // In STDINMode, we override the file content with the <stdin> input. |
| 65 | // Since `tool.mapVirtualFile` takes `StringRef`, we define `Code` outside of |
| 66 | // the if-block so that `Code` is not released after the if-block. |
| 67 | std::unique_ptr<llvm::MemoryBuffer> Code; |
| 68 | if (STDINMode) { |
| 69 | assert(options.getSourcePathList().size() == 1 && |
| 70 | "Expect exactly one file path in STDINMode."); |
| 71 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CodeOrErr = |
| 72 | MemoryBuffer::getSTDIN(); |
| 73 | if (std::error_code EC = CodeOrErr.getError()) { |
| 74 | errs() << EC.message() << "\n"; |
| 75 | return 1; |
| 76 | } |
| 77 | Code = std::move(CodeOrErr.get()); |
| 78 | if (Code->getBufferSize() == 0) |
| 79 | return 0; // Skip empty files. |
| 80 | |
| 81 | tool.mapVirtualFile(options.getSourcePathList().front(), Code->getBuffer()); |
| 82 | } |
| 83 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 84 | // Set up data source. |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 85 | auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>(); |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 86 | switch (DatabaseFormat) { |
| 87 | case fixed: { |
| 88 | // Parse input and fill the database with it. |
| 89 | // <symbol>=<header><, header...> |
| 90 | // Multiple symbols can be given, separated by semicolons. |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 91 | std::map<std::string, std::vector<std::string>> SymbolsMap; |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 92 | SmallVector<StringRef, 4> SemicolonSplits; |
| 93 | StringRef(Input).split(SemicolonSplits, ";"); |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 94 | std::vector<find_all_symbols::SymbolInfo> Symbols; |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 95 | for (StringRef Pair : SemicolonSplits) { |
| 96 | auto Split = Pair.split('='); |
| 97 | std::vector<std::string> Headers; |
| 98 | SmallVector<StringRef, 4> CommaSplits; |
| 99 | Split.second.split(CommaSplits, ","); |
| 100 | for (StringRef Header : CommaSplits) |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 101 | Symbols.push_back(find_all_symbols::SymbolInfo( |
| 102 | Split.first.trim(), |
| 103 | find_all_symbols::SymbolInfo::SymbolKind::Unknown, Header.trim(), 1, |
| 104 | {})); |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 105 | } |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 106 | SymbolIndexMgr->addSymbolIndex( |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 107 | llvm::make_unique<include_fixer::InMemorySymbolIndex>(Symbols)); |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 108 | break; |
| 109 | } |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 110 | case yaml: { |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 111 | llvm::ErrorOr<std::unique_ptr<include_fixer::YamlSymbolIndex>> DB(nullptr); |
Benjamin Kramer | b6aed5f4 | 2016-05-09 14:14:55 +0000 | [diff] [blame] | 112 | if (!Input.empty()) { |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 113 | DB = include_fixer::YamlSymbolIndex::createFromFile(Input); |
Benjamin Kramer | b6aed5f4 | 2016-05-09 14:14:55 +0000 | [diff] [blame] | 114 | } else { |
| 115 | // If we don't have any input file, look in the directory of the first |
| 116 | // file and its parents. |
| 117 | SmallString<128> AbsolutePath( |
| 118 | tooling::getAbsolutePath(options.getSourcePathList().front())); |
| 119 | StringRef Directory = llvm::sys::path::parent_path(AbsolutePath); |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 120 | DB = include_fixer::YamlSymbolIndex::createFromDirectory( |
Benjamin Kramer | b6aed5f4 | 2016-05-09 14:14:55 +0000 | [diff] [blame] | 121 | Directory, "find_all_symbols_db.yaml"); |
| 122 | } |
| 123 | |
| 124 | if (!DB) { |
| 125 | llvm::errs() << "Couldn't find YAML db: " << DB.getError().message() |
| 126 | << '\n'; |
| 127 | return 1; |
| 128 | } |
| 129 | |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 130 | SymbolIndexMgr->addSymbolIndex(std::move(*DB)); |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 131 | break; |
| 132 | } |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 133 | } |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 134 | |
| 135 | // Now run our tool. |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 136 | std::vector<tooling::Replacement> Replacements; |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 137 | include_fixer::IncludeFixerActionFactory Factory( |
| 138 | *SymbolIndexMgr, Replacements, MinimizeIncludePaths); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 139 | |
Benjamin Kramer | 6b5160a | 2016-05-18 13:32:38 +0000 | [diff] [blame] | 140 | if (tool.run(&Factory) != 0) { |
| 141 | llvm::errs() |
| 142 | << "Clang died with a fatal error! (incorrect include paths?)\n"; |
| 143 | return 1; |
| 144 | } |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 145 | |
Benjamin Kramer | 5471829 | 2016-05-10 08:36:56 +0000 | [diff] [blame] | 146 | if (!Quiet) |
| 147 | for (const tooling::Replacement &Replacement : Replacements) |
| 148 | llvm::errs() << "Added " << Replacement.getReplacementText(); |
| 149 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 150 | // Set up a new source manager for applying the resulting replacements. |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 151 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); |
| 152 | DiagnosticsEngine Diagnostics(new DiagnosticIDs, &*DiagOpts); |
| 153 | TextDiagnosticPrinter DiagnosticPrinter(outs(), &*DiagOpts); |
| 154 | SourceManager SM(Diagnostics, tool.getFiles()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 155 | Diagnostics.setClient(&DiagnosticPrinter, false); |
| 156 | |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame^] | 157 | if (STDINMode) { |
| 158 | for (const tooling::Replacement &Replacement : Replacements) { |
| 159 | FileID ID = SM.getMainFileID(); |
| 160 | unsigned LineNum = SM.getLineNumber(ID, Replacement.getOffset()); |
| 161 | llvm::outs() << LineNum << "," << Replacement.getReplacementText(); |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 166 | // Write replacements to disk. |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 167 | Rewriter Rewrites(SM, LangOptions()); |
| 168 | tooling::applyAllReplacements(Replacements, Rewrites); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 169 | return Rewrites.overwriteChangedFiles(); |
| 170 | } |
Haojian Wu | a315dcb | 2016-05-03 08:38:35 +0000 | [diff] [blame] | 171 | |
| 172 | } // namespace |
| 173 | |
| 174 | int main(int argc, const char **argv) { |
| 175 | return includeFixerMain(argc, argv); |
| 176 | } |