2012-12-02 13:20:44 +00:00
|
|
|
//===--- CommonOptionsParser.cpp - common options for clang tools ---------===//
|
2012-08-15 20:02:54 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-12-02 13:20:44 +00:00
|
|
|
// This file implements the CommonOptionsParser class used to parse common
|
|
|
|
// command-line options for clang tools, so that they can be run as separate
|
|
|
|
// command-line applications with a consistent common interface for handling
|
|
|
|
// compilation database and input files.
|
2012-08-15 20:02:54 +00:00
|
|
|
//
|
|
|
|
// It provides a common subset of command-line options, common algorithm
|
|
|
|
// for locating a compilation database and source files, and help messages
|
|
|
|
// for the basic command-line interface.
|
|
|
|
//
|
2012-12-02 13:20:44 +00:00
|
|
|
// It creates a CompilationDatabase and reads common command-line options.
|
|
|
|
//
|
|
|
|
// This class uses the Clang Tooling infrastructure, see
|
|
|
|
// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
|
|
|
|
// for details on setting it up with LLVM source tree.
|
2012-08-15 20:02:54 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-02 13:20:44 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
2012-08-15 20:02:54 +00:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
|
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-12-02 13:20:44 +00:00
|
|
|
const char *const CommonOptionsParser::HelpMessage =
|
2012-08-15 20:02:54 +00:00
|
|
|
"\n"
|
|
|
|
"-p <build-path> is used to read a compile command database.\n"
|
|
|
|
"\n"
|
|
|
|
"\tFor example, it can be a CMake build directory in which a file named\n"
|
|
|
|
"\tcompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n"
|
|
|
|
"\tCMake option to get this output). When no build path is specified,\n"
|
2013-04-08 18:45:10 +00:00
|
|
|
"\ta search for compile_commands.json will be attempted through all\n"
|
|
|
|
"\tparent paths of the first input file . See:\n"
|
2012-08-15 20:02:54 +00:00
|
|
|
"\thttp://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n"
|
|
|
|
"\texample of setting up Clang Tooling on a source tree.\n"
|
|
|
|
"\n"
|
2012-12-02 13:20:44 +00:00
|
|
|
"<source0> ... specify the paths of source files. These paths are\n"
|
|
|
|
"\tlooked up in the compile command database. If the path of a file is\n"
|
|
|
|
"\tabsolute, it needs to point into CMake's source tree. If the path is\n"
|
|
|
|
"\trelative, the current working directory needs to be in the CMake\n"
|
|
|
|
"\tsource tree and the file must be in a subdirectory of the current\n"
|
|
|
|
"\tworking directory. \"./\" prefixes in the relative files will be\n"
|
|
|
|
"\tautomatically removed, but the rest of a relative path must be a\n"
|
|
|
|
"\tsuffix of a path in the compile command database.\n"
|
2012-08-15 20:02:54 +00:00
|
|
|
"\n";
|
|
|
|
|
2013-12-22 00:07:40 +00:00
|
|
|
CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv,
|
2014-11-24 09:15:30 +00:00
|
|
|
cl::OptionCategory &Category,
|
2013-12-22 00:07:40 +00:00
|
|
|
const char *Overview) {
|
2014-11-24 09:15:30 +00:00
|
|
|
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<std::string> BuildPath("p", cl::desc("Build path"),
|
|
|
|
cl::Optional, cl::cat(Category));
|
2012-12-02 13:20:44 +00:00
|
|
|
|
|
|
|
static cl::list<std::string> SourcePaths(
|
2014-11-24 09:15:30 +00:00
|
|
|
cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore,
|
|
|
|
cl::cat(Category));
|
|
|
|
|
|
|
|
// Hide unrelated options.
|
|
|
|
StringMap<cl::Option*> Options;
|
|
|
|
cl::getRegisteredOptions(Options);
|
|
|
|
for (StringMap<cl::Option *>::iterator I = Options.begin(), E = Options.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->second->Category != &Category && I->first() != "help" &&
|
|
|
|
I->first() != "version")
|
|
|
|
I->second->setHiddenFlag(cl::ReallyHidden);
|
|
|
|
}
|
2012-08-15 20:02:54 +00:00
|
|
|
|
2012-12-02 13:20:44 +00:00
|
|
|
Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc,
|
|
|
|
argv));
|
2013-12-22 00:07:40 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, Overview);
|
2012-12-02 13:20:44 +00:00
|
|
|
SourcePathList = SourcePaths;
|
2012-08-15 20:02:54 +00:00
|
|
|
if (!Compilations) {
|
|
|
|
std::string ErrorMessage;
|
|
|
|
if (!BuildPath.empty()) {
|
|
|
|
Compilations.reset(CompilationDatabase::autoDetectFromDirectory(
|
|
|
|
BuildPath, ErrorMessage));
|
|
|
|
} else {
|
|
|
|
Compilations.reset(CompilationDatabase::autoDetectFromSource(
|
|
|
|
SourcePaths[0], ErrorMessage));
|
|
|
|
}
|
|
|
|
if (!Compilations)
|
|
|
|
llvm::report_fatal_error(ErrorMessage);
|
|
|
|
}
|
|
|
|
}
|