#include "FuzzySymbolIndex.h"
#include "InMemorySymbolIndex.h"
#include "IncludeFixer.h"
#include "IncludeFixerContext.h"
#include "SymbolIndexManager.h"
#include "YamlSymbolIndex.h"
#include "clang/Format/Format.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Core/Replacement.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLTraits.h"
usingnamespaceclang;
usingnamespacellvm;
IncludeFixerContext;
LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(…)
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(IncludeFixerContext::HeaderInfo)
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(IncludeFixerContext::QuerySymbolInfo)
namespace llvm {
namespace yaml {
template <> struct MappingTraits<tooling::Range> { … };
template <> struct MappingTraits<IncludeFixerContext::HeaderInfo> { … };
template <> struct MappingTraits<IncludeFixerContext::QuerySymbolInfo> { … };
template <> struct MappingTraits<IncludeFixerContext> { … };
}
}
namespace {
cl::OptionCategory IncludeFixerCategory("Tool options");
enum DatabaseFormatTy { … };
cl::opt<DatabaseFormatTy> DatabaseFormat(
"db", cl::desc("Specify input format"),
cl::values(clEnumVal(fixed, "Hard-coded mapping"),
clEnumVal(yaml, "Yaml database created by find-all-symbols"),
clEnumVal(fuzzyYaml, "Yaml database, with fuzzy-matched names")),
cl::init(yaml), cl::cat(IncludeFixerCategory));
cl::opt<std::string> Input("input",
cl::desc("String to initialize the database"),
cl::cat(IncludeFixerCategory));
cl::opt<std::string>
QuerySymbol("query-symbol",
cl::desc("Query a given symbol (e.g. \"a::b::foo\") in\n"
"database directly without parsing the file."),
cl::cat(IncludeFixerCategory));
cl::opt<bool>
MinimizeIncludePaths("minimize-paths",
cl::desc("Whether to minimize added include paths"),
cl::init(true), cl::cat(IncludeFixerCategory));
cl::opt<bool> Quiet("q", cl::desc("Reduce terminal output"), cl::init(false),
cl::cat(IncludeFixerCategory));
cl::opt<bool>
STDINMode("stdin",
cl::desc("Override source file's content (in the overlaying\n"
"virtual file system) with input from <stdin> and run\n"
"the tool on the new content with the compilation\n"
"options of the source file. This mode is currently\n"
"used for editor integration."),
cl::init(false), cl::cat(IncludeFixerCategory));
cl::opt<bool> OutputHeaders(
"output-headers",
cl::desc("Print the symbol being queried and all its relevant headers in\n"
"JSON format to stdout:\n"
" {\n"
" \"FilePath\": \"/path/to/foo.cc\",\n"
" \"QuerySymbolInfos\": [\n"
" {\"RawIdentifier\": \"foo\",\n"
" \"Range\": {\"Offset\": 0, \"Length\": 3}}\n"
" ],\n"
" \"HeaderInfos\": [ {\"Header\": \"\\\"foo_a.h\\\"\",\n"
" \"QualifiedName\": \"a::foo\"} ]\n"
" }"),
cl::init(false), cl::cat(IncludeFixerCategory));
cl::opt<std::string> InsertHeader(
"insert-header",
cl::desc("Insert a specific header. This should run with STDIN mode.\n"
"The result is written to stdout. It is currently used for\n"
"editor integration. Support YAML/JSON format:\n"
" -insert-header=\"{\n"
" FilePath: \"/path/to/foo.cc\",\n"
" QuerySymbolInfos: [\n"
" {RawIdentifier: foo,\n"
" Range: {Offset: 0, Length: 3}}\n"
" ],\n"
" HeaderInfos: [ {Headers: \"\\\"foo_a.h\\\"\",\n"
" QualifiedName: \"a::foo\"} ]}\""),
cl::init(""), cl::cat(IncludeFixerCategory));
cl::opt<std::string>
Style("style",
cl::desc("Fallback style for reformatting after inserting new\n"
"headers if there is no clang-format config file found."),
cl::init("llvm"), cl::cat(IncludeFixerCategory));
std::unique_ptr<include_fixer::SymbolIndexManager>
createSymbolIndexManager(StringRef FilePath) { … }
void writeToJson(llvm::raw_ostream &OS, const IncludeFixerContext& Context) { … }
int includeFixerMain(int argc, const char **argv) { … }
}
int main(int argc, const char **argv) { … }