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" |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 12 | #include "IncludeFixerContext.h" |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 13 | #include "SymbolIndexManager.h" |
| 14 | #include "YamlSymbolIndex.h" |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 15 | #include "clang/Format/Format.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 17 | #include "clang/Rewrite/Core/Rewriter.h" |
| 18 | #include "clang/Tooling/CommonOptionsParser.h" |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 19 | #include "clang/Tooling/Core/Replacement.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 20 | #include "clang/Tooling/Tooling.h" |
| 21 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | 8ff8fdf | 2016-07-18 19:21:22 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Path.h" |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 23 | #include "llvm/Support/YAMLTraits.h" |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 24 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 25 | using namespace clang; |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 27 | using clang::include_fixer::IncludeFixerContext; |
| 28 | |
| 29 | LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(IncludeFixerContext) |
| 30 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string) |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 31 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(IncludeFixerContext::HeaderInfo) |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 32 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(IncludeFixerContext::QuerySymbolInfo) |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 33 | |
| 34 | namespace llvm { |
| 35 | namespace yaml { |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 36 | |
| 37 | template <> struct MappingTraits<tooling::Range> { |
| 38 | struct NormalizedRange { |
| 39 | NormalizedRange(const IO &) : Offset(0), Length(0) {} |
| 40 | |
| 41 | NormalizedRange(const IO &, const tooling::Range &R) |
| 42 | : Offset(R.getOffset()), Length(R.getLength()) {} |
| 43 | |
| 44 | tooling::Range denormalize(const IO &) { |
| 45 | return tooling::Range(Offset, Length); |
| 46 | } |
| 47 | |
| 48 | unsigned Offset; |
| 49 | unsigned Length; |
| 50 | }; |
| 51 | static void mapping(IO &IO, tooling::Range &Info) { |
| 52 | MappingNormalization<NormalizedRange, tooling::Range> Keys(IO, Info); |
| 53 | IO.mapRequired("Offset", Keys->Offset); |
| 54 | IO.mapRequired("Length", Keys->Length); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | template <> struct MappingTraits<IncludeFixerContext::HeaderInfo> { |
| 59 | static void mapping(IO &io, IncludeFixerContext::HeaderInfo &Info) { |
| 60 | io.mapRequired("Header", Info.Header); |
| 61 | io.mapRequired("QualifiedName", Info.QualifiedName); |
| 62 | } |
| 63 | }; |
| 64 | |
Haojian Wu | 9e4bd0c | 2016-07-19 14:49:04 +0000 | [diff] [blame] | 65 | template <> struct MappingTraits<IncludeFixerContext::QuerySymbolInfo> { |
| 66 | static void mapping(IO &io, IncludeFixerContext::QuerySymbolInfo &Info) { |
| 67 | io.mapRequired("RawIdentifier", Info.RawIdentifier); |
| 68 | io.mapRequired("Range", Info.Range); |
| 69 | } |
| 70 | }; |
| 71 | |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 72 | template <> struct MappingTraits<IncludeFixerContext> { |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 73 | static void mapping(IO &IO, IncludeFixerContext &Context) { |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 74 | IO.mapRequired("QuerySymbolInfos", Context.QuerySymbolInfos); |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 75 | IO.mapRequired("HeaderInfos", Context.HeaderInfos); |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 76 | IO.mapRequired("FilePath", Context.FilePath); |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 77 | } |
| 78 | }; |
| 79 | } // namespace yaml |
| 80 | } // namespace llvm |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 81 | |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 82 | namespace { |
| 83 | cl::OptionCategory IncludeFixerCategory("Tool options"); |
| 84 | |
| 85 | enum DatabaseFormatTy { |
Simon Pilgrim | b9f2558 | 2016-04-27 20:43:32 +0000 | [diff] [blame] | 86 | fixed, ///< Hard-coded mapping. |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 87 | yaml, ///< Yaml database created by find-all-symbols. |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | cl::opt<DatabaseFormatTy> DatabaseFormat( |
| 91 | "db", cl::desc("Specify input format"), |
Haojian Wu | d8c12ba | 2016-04-29 09:23:38 +0000 | [diff] [blame] | 92 | cl::values(clEnumVal(fixed, "Hard-coded mapping"), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 93 | clEnumVal(yaml, "Yaml database created by find-all-symbols")), |
Benjamin Kramer | 8fd85a5 | 2016-05-10 11:35:47 +0000 | [diff] [blame] | 94 | cl::init(yaml), cl::cat(IncludeFixerCategory)); |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 95 | |
| 96 | cl::opt<std::string> Input("input", |
| 97 | cl::desc("String to initialize the database"), |
| 98 | cl::cat(IncludeFixerCategory)); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 99 | |
Haojian Wu | cd63701 | 2016-09-07 16:34:35 +0000 | [diff] [blame] | 100 | cl::opt<std::string> |
| 101 | QuerySymbol("query-symbol", |
| 102 | cl::desc("Query a given symbol (e.g. \"a::b::foo\") in\n" |
| 103 | "database directly without parsing the file."), |
| 104 | cl::cat(IncludeFixerCategory)); |
| 105 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 106 | cl::opt<bool> |
| 107 | MinimizeIncludePaths("minimize-paths", |
| 108 | cl::desc("Whether to minimize added include paths"), |
| 109 | cl::init(true), cl::cat(IncludeFixerCategory)); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 110 | |
Benjamin Kramer | 5471829 | 2016-05-10 08:36:56 +0000 | [diff] [blame] | 111 | cl::opt<bool> Quiet("q", cl::desc("Reduce terminal output"), cl::init(false), |
| 112 | cl::cat(IncludeFixerCategory)); |
| 113 | |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 114 | cl::opt<bool> |
| 115 | STDINMode("stdin", |
| 116 | cl::desc("Override source file's content (in the overlaying\n" |
| 117 | "virtual file system) with input from <stdin> and run\n" |
| 118 | "the tool on the new content with the compilation\n" |
| 119 | "options of the source file. This mode is currently\n" |
| 120 | "used for editor integration."), |
| 121 | cl::init(false), cl::cat(IncludeFixerCategory)); |
| 122 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 123 | cl::opt<bool> OutputHeaders( |
| 124 | "output-headers", |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 125 | cl::desc("Print the symbol being queried and all its relevant headers in\n" |
| 126 | "JSON format to stdout:\n" |
| 127 | " {\n" |
Haojian Wu | ab37264 | 2016-08-17 11:31:19 +0000 | [diff] [blame] | 128 | " \"FilePath\": \"/path/to/foo.cc\",\n" |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 129 | " \"QuerySymbolInfos\": [\n" |
| 130 | " {\"RawIdentifier\": \"foo\",\n" |
| 131 | " \"Range\": {\"Offset\": 0, \"Length\": 3}}\n" |
| 132 | " ],\n" |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 133 | " \"HeaderInfos\": [ {\"Header\": \"\\\"foo_a.h\\\"\",\n" |
| 134 | " \"QualifiedName\": \"a::foo\"} ]\n" |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 135 | " }"), |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 136 | cl::init(false), cl::cat(IncludeFixerCategory)); |
| 137 | |
| 138 | cl::opt<std::string> InsertHeader( |
| 139 | "insert-header", |
| 140 | cl::desc("Insert a specific header. This should run with STDIN mode.\n" |
| 141 | "The result is written to stdout. It is currently used for\n" |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 142 | "editor integration. Support YAML/JSON format:\n" |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 143 | " -insert-header=\"{\n" |
Haojian Wu | ab37264 | 2016-08-17 11:31:19 +0000 | [diff] [blame] | 144 | " FilePath: \"/path/to/foo.cc\",\n" |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 145 | " QuerySymbolInfos: [\n" |
| 146 | " {RawIdentifier: foo,\n" |
| 147 | " Range: {Offset: 0, Length: 3}}\n" |
| 148 | " ],\n" |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 149 | " HeaderInfos: [ {Headers: \"\\\"foo_a.h\\\"\",\n" |
| 150 | " QualifiedName: \"a::foo\"} ]}\""), |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 151 | cl::init(""), cl::cat(IncludeFixerCategory)); |
| 152 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 153 | cl::opt<std::string> |
| 154 | Style("style", |
Haojian Wu | ab37264 | 2016-08-17 11:31:19 +0000 | [diff] [blame] | 155 | cl::desc("Fallback style for reformatting after inserting new\n" |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 156 | "headers if there is no clang-format config file found."), |
| 157 | cl::init("llvm"), cl::cat(IncludeFixerCategory)); |
| 158 | |
Haojian Wu | eb6ce06 | 2016-05-31 13:23:00 +0000 | [diff] [blame] | 159 | std::unique_ptr<include_fixer::SymbolIndexManager> |
| 160 | createSymbolIndexManager(StringRef FilePath) { |
| 161 | auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>(); |
| 162 | switch (DatabaseFormat) { |
| 163 | case fixed: { |
| 164 | // Parse input and fill the database with it. |
| 165 | // <symbol>=<header><, header...> |
| 166 | // Multiple symbols can be given, separated by semicolons. |
| 167 | std::map<std::string, std::vector<std::string>> SymbolsMap; |
| 168 | SmallVector<StringRef, 4> SemicolonSplits; |
| 169 | StringRef(Input).split(SemicolonSplits, ";"); |
| 170 | std::vector<find_all_symbols::SymbolInfo> Symbols; |
| 171 | for (StringRef Pair : SemicolonSplits) { |
| 172 | auto Split = Pair.split('='); |
| 173 | std::vector<std::string> Headers; |
| 174 | SmallVector<StringRef, 4> CommaSplits; |
| 175 | Split.second.split(CommaSplits, ","); |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame] | 176 | for (size_t I = 0, E = CommaSplits.size(); I != E; ++I) |
Haojian Wu | eb6ce06 | 2016-05-31 13:23:00 +0000 | [diff] [blame] | 177 | Symbols.push_back(find_all_symbols::SymbolInfo( |
| 178 | Split.first.trim(), |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame] | 179 | find_all_symbols::SymbolInfo::SymbolKind::Unknown, |
| 180 | CommaSplits[I].trim(), 1, {}, /*NumOccurrences=*/E - I)); |
Haojian Wu | eb6ce06 | 2016-05-31 13:23:00 +0000 | [diff] [blame] | 181 | } |
| 182 | SymbolIndexMgr->addSymbolIndex( |
| 183 | llvm::make_unique<include_fixer::InMemorySymbolIndex>(Symbols)); |
| 184 | break; |
| 185 | } |
| 186 | case yaml: { |
| 187 | llvm::ErrorOr<std::unique_ptr<include_fixer::YamlSymbolIndex>> DB(nullptr); |
| 188 | if (!Input.empty()) { |
| 189 | DB = include_fixer::YamlSymbolIndex::createFromFile(Input); |
| 190 | } else { |
| 191 | // If we don't have any input file, look in the directory of the first |
| 192 | // file and its parents. |
| 193 | SmallString<128> AbsolutePath(tooling::getAbsolutePath(FilePath)); |
| 194 | StringRef Directory = llvm::sys::path::parent_path(AbsolutePath); |
| 195 | DB = include_fixer::YamlSymbolIndex::createFromDirectory( |
| 196 | Directory, "find_all_symbols_db.yaml"); |
| 197 | } |
| 198 | |
| 199 | if (!DB) { |
| 200 | llvm::errs() << "Couldn't find YAML db: " << DB.getError().message() |
| 201 | << '\n'; |
| 202 | return nullptr; |
| 203 | } |
| 204 | |
| 205 | SymbolIndexMgr->addSymbolIndex(std::move(*DB)); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | return SymbolIndexMgr; |
| 210 | } |
| 211 | |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 212 | void writeToJson(llvm::raw_ostream &OS, const IncludeFixerContext& Context) { |
| 213 | OS << "{\n" |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 214 | << " \"FilePath\": \"" |
| 215 | << llvm::yaml::escape(Context.getFilePath()) << "\",\n" |
| 216 | << " \"QuerySymbolInfos\": [\n"; |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 217 | for (const auto &Info : Context.getQuerySymbolInfos()) { |
| 218 | OS << " {\"RawIdentifier\": \"" << Info.RawIdentifier << "\",\n"; |
| 219 | OS << " \"Range\":{"; |
| 220 | OS << "\"Offset\":" << Info.Range.getOffset() << ","; |
| 221 | OS << "\"Length\":" << Info.Range.getLength() << "}}"; |
| 222 | if (&Info != &Context.getQuerySymbolInfos().back()) |
| 223 | OS << ",\n"; |
| 224 | } |
| 225 | OS << "\n ],\n"; |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 226 | OS << " \"HeaderInfos\": [\n"; |
| 227 | const auto &HeaderInfos = Context.getHeaderInfos(); |
| 228 | for (const auto &Info : HeaderInfos) { |
| 229 | OS << " {\"Header\": \"" << llvm::yaml::escape(Info.Header) << "\",\n" |
| 230 | << " \"QualifiedName\": \"" << Info.QualifiedName << "\"}"; |
| 231 | if (&Info != &HeaderInfos.back()) |
| 232 | OS << ",\n"; |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 233 | } |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 234 | OS << "\n"; |
| 235 | OS << " ]\n"; |
| 236 | OS << "}\n"; |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Haojian Wu | a315dcb | 2016-05-03 08:38:35 +0000 | [diff] [blame] | 239 | int includeFixerMain(int argc, const char **argv) { |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 240 | tooling::CommonOptionsParser options(argc, argv, IncludeFixerCategory); |
| 241 | tooling::ClangTool tool(options.getCompilations(), |
| 242 | options.getSourcePathList()); |
| 243 | |
Haojian Wu | cd63701 | 2016-09-07 16:34:35 +0000 | [diff] [blame] | 244 | llvm::StringRef SourceFilePath = options.getSourcePathList().front(); |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 245 | // In STDINMode, we override the file content with the <stdin> input. |
| 246 | // Since `tool.mapVirtualFile` takes `StringRef`, we define `Code` outside of |
| 247 | // the if-block so that `Code` is not released after the if-block. |
| 248 | std::unique_ptr<llvm::MemoryBuffer> Code; |
| 249 | if (STDINMode) { |
| 250 | assert(options.getSourcePathList().size() == 1 && |
| 251 | "Expect exactly one file path in STDINMode."); |
| 252 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CodeOrErr = |
| 253 | MemoryBuffer::getSTDIN(); |
| 254 | if (std::error_code EC = CodeOrErr.getError()) { |
| 255 | errs() << EC.message() << "\n"; |
| 256 | return 1; |
| 257 | } |
| 258 | Code = std::move(CodeOrErr.get()); |
| 259 | if (Code->getBufferSize() == 0) |
| 260 | return 0; // Skip empty files. |
| 261 | |
Haojian Wu | cd63701 | 2016-09-07 16:34:35 +0000 | [diff] [blame] | 262 | tool.mapVirtualFile(SourceFilePath, Code->getBuffer()); |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 265 | if (!InsertHeader.empty()) { |
| 266 | if (!STDINMode) { |
| 267 | errs() << "Should be running in STDIN mode\n"; |
| 268 | return 1; |
| 269 | } |
| 270 | |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 271 | llvm::yaml::Input yin(InsertHeader); |
| 272 | IncludeFixerContext Context; |
| 273 | yin >> Context; |
| 274 | |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 275 | const auto &HeaderInfos = Context.getHeaderInfos(); |
| 276 | assert(!HeaderInfos.empty()); |
| 277 | // We only accept one unique header. |
| 278 | // Check all elements in HeaderInfos have the same header. |
| 279 | bool IsUniqueHeader = std::equal( |
| 280 | HeaderInfos.begin()+1, HeaderInfos.end(), HeaderInfos.begin(), |
| 281 | [](const IncludeFixerContext::HeaderInfo &LHS, |
| 282 | const IncludeFixerContext::HeaderInfo &RHS) { |
| 283 | return LHS.Header == RHS.Header; |
| 284 | }); |
| 285 | if (!IsUniqueHeader) { |
| 286 | errs() << "Expect exactly one unique header.\n"; |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 287 | return 1; |
| 288 | } |
| 289 | |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 290 | // If a header has multiple symbols, we won't add the missing namespace |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 291 | // qualifiers because we don't know which one is exactly used. |
| 292 | // |
| 293 | // Check whether all elements in HeaderInfos have the same qualified name. |
| 294 | bool IsUniqueQualifiedName = std::equal( |
| 295 | HeaderInfos.begin() + 1, HeaderInfos.end(), HeaderInfos.begin(), |
| 296 | [](const IncludeFixerContext::HeaderInfo &LHS, |
| 297 | const IncludeFixerContext::HeaderInfo &RHS) { |
| 298 | return LHS.QualifiedName == RHS.QualifiedName; |
| 299 | }); |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 300 | format::FormatStyle InsertStyle = |
| 301 | format::getStyle("file", Context.getFilePath(), Style); |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 302 | auto Replacements = clang::include_fixer::createIncludeFixerReplacements( |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 303 | Code->getBuffer(), Context, InsertStyle, |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 304 | /*AddQualifiers=*/IsUniqueQualifiedName); |
| 305 | if (!Replacements) { |
| 306 | errs() << "Failed to create replacements: " |
| 307 | << llvm::toString(Replacements.takeError()) << "\n"; |
| 308 | return 1; |
| 309 | } |
| 310 | |
Eric Liu | a452db4 | 2016-07-11 13:53:21 +0000 | [diff] [blame] | 311 | auto ChangedCode = |
| 312 | tooling::applyAllReplacements(Code->getBuffer(), *Replacements); |
| 313 | if (!ChangedCode) { |
| 314 | llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n"; |
| 315 | return 1; |
| 316 | } |
| 317 | llvm::outs() << *ChangedCode; |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 321 | // Set up data source. |
Haojian Wu | eb6ce06 | 2016-05-31 13:23:00 +0000 | [diff] [blame] | 322 | std::unique_ptr<include_fixer::SymbolIndexManager> SymbolIndexMgr = |
Haojian Wu | cd63701 | 2016-09-07 16:34:35 +0000 | [diff] [blame] | 323 | createSymbolIndexManager(SourceFilePath); |
Haojian Wu | eb6ce06 | 2016-05-31 13:23:00 +0000 | [diff] [blame] | 324 | if (!SymbolIndexMgr) |
| 325 | return 1; |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 326 | |
Haojian Wu | cd63701 | 2016-09-07 16:34:35 +0000 | [diff] [blame] | 327 | // Query symbol mode. |
| 328 | if (!QuerySymbol.empty()) { |
| 329 | auto MatchedSymbols = SymbolIndexMgr->search(QuerySymbol); |
| 330 | for (auto &Symbol : MatchedSymbols) { |
| 331 | std::string HeaderPath = Symbol.getFilePath().str(); |
| 332 | Symbol.SetFilePath(((HeaderPath[0] == '"' || HeaderPath[0] == '<') |
| 333 | ? HeaderPath |
| 334 | : "\"" + HeaderPath + "\"")); |
| 335 | } |
| 336 | |
| 337 | // We leave an empty symbol range as we don't know the range of the symbol |
| 338 | // being queried in this mode. include-fixer won't add namespace qualifiers |
| 339 | // if the symbol range is empty, which also fits this case. |
| 340 | IncludeFixerContext::QuerySymbolInfo Symbol; |
| 341 | Symbol.RawIdentifier = QuerySymbol; |
| 342 | auto Context = |
| 343 | IncludeFixerContext(SourceFilePath, {Symbol}, MatchedSymbols); |
| 344 | writeToJson(llvm::outs(), Context); |
| 345 | return 0; |
| 346 | } |
| 347 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 348 | // Now run our tool. |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 349 | std::vector<include_fixer::IncludeFixerContext> Contexts; |
| 350 | include_fixer::IncludeFixerActionFactory Factory(*SymbolIndexMgr, Contexts, |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 351 | Style, MinimizeIncludePaths); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 352 | |
Benjamin Kramer | 6b5160a | 2016-05-18 13:32:38 +0000 | [diff] [blame] | 353 | if (tool.run(&Factory) != 0) { |
| 354 | llvm::errs() |
| 355 | << "Clang died with a fatal error! (incorrect include paths?)\n"; |
| 356 | return 1; |
| 357 | } |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 358 | |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 359 | assert(!Contexts.empty()); |
| 360 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 361 | if (OutputHeaders) { |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 362 | // FIXME: Print contexts of all processing files instead of the first one. |
| 363 | writeToJson(llvm::outs(), Contexts.front()); |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 367 | std::vector<tooling::Replacements> FixerReplacements; |
| 368 | for (const auto &Context : Contexts) { |
| 369 | StringRef FilePath = Context.getFilePath(); |
| 370 | format::FormatStyle InsertStyle = format::getStyle("file", FilePath, Style); |
| 371 | auto Buffer = llvm::MemoryBuffer::getFile(FilePath); |
| 372 | if (!Buffer) { |
| 373 | errs() << "Couldn't open file: " + FilePath.str() + ": " |
| 374 | << Buffer.getError().message() + "\n"; |
| 375 | return 1; |
| 376 | } |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 377 | |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 378 | auto Replacements = clang::include_fixer::createIncludeFixerReplacements( |
| 379 | Buffer.get()->getBuffer(), Context, InsertStyle); |
| 380 | if (!Replacements) { |
| 381 | errs() << "Failed to create replacement: " |
| 382 | << llvm::toString(Replacements.takeError()) << "\n"; |
| 383 | return 1; |
| 384 | } |
| 385 | FixerReplacements.push_back(*Replacements); |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 388 | if (!Quiet) { |
| 389 | for (const auto &Context : Contexts) { |
| 390 | if (!Context.getHeaderInfos().empty()) { |
| 391 | llvm::errs() << "Added #include " |
| 392 | << Context.getHeaderInfos().front().Header << " for " |
| 393 | << Context.getFilePath() << "\n"; |
| 394 | } |
| 395 | } |
Eric Liu | a452db4 | 2016-07-11 13:53:21 +0000 | [diff] [blame] | 396 | } |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 397 | |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 398 | if (STDINMode) { |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 399 | assert(FixerReplacements.size() == 1); |
| 400 | auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), |
| 401 | FixerReplacements.front()); |
Eric Liu | a452db4 | 2016-07-11 13:53:21 +0000 | [diff] [blame] | 402 | if (!ChangedCode) { |
| 403 | llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n"; |
| 404 | return 1; |
| 405 | } |
| 406 | llvm::outs() << *ChangedCode; |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 410 | // Set up a new source manager for applying the resulting replacements. |
| 411 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); |
| 412 | DiagnosticsEngine Diagnostics(new DiagnosticIDs, &*DiagOpts); |
| 413 | TextDiagnosticPrinter DiagnosticPrinter(outs(), &*DiagOpts); |
| 414 | SourceManager SM(Diagnostics, tool.getFiles()); |
| 415 | Diagnostics.setClient(&DiagnosticPrinter, false); |
| 416 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 417 | // Write replacements to disk. |
Benjamin Kramer | f412e90 | 2016-04-27 14:24:32 +0000 | [diff] [blame] | 418 | Rewriter Rewrites(SM, LangOptions()); |
Benjamin Kramer | 089a39e | 2016-10-19 13:50:17 +0000 | [diff] [blame^] | 419 | for (const auto &Replacement : FixerReplacements) { |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 420 | if (!tooling::applyAllReplacements(Replacement, Rewrites)) { |
| 421 | llvm::errs() << "Failed to apply replacements.\n"; |
| 422 | return 1; |
| 423 | } |
| 424 | } |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 425 | return Rewrites.overwriteChangedFiles(); |
| 426 | } |
Haojian Wu | a315dcb | 2016-05-03 08:38:35 +0000 | [diff] [blame] | 427 | |
| 428 | } // namespace |
| 429 | |
| 430 | int main(int argc, const char **argv) { |
| 431 | return includeFixerMain(argc, argv); |
| 432 | } |