2012-08-23 04:52:52 +08:00
|
|
|
//===--- CommonOptionsParser.cpp - common options for clang tools ---------===//
|
2012-07-16 20:46:48 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-07-16 20:46:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-08-23 04:52:52 +08: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-07-16 20:46:48 +08: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-08-23 04:52:52 +08: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-07-16 20:46:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-23 04:52:52 +08:00
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
2012-07-16 20:46:48 +08:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
2017-10-24 21:10:58 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-07-16 20:46:48 +08:00
|
|
|
|
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-08-24 08:39:14 +08:00
|
|
|
const char *const CommonOptionsParser::HelpMessage =
|
2012-07-16 20:46:48 +08: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"
|
2012-12-15 02:58:25 +08:00
|
|
|
"\ta search for compile_commands.json will be attempted through all\n"
|
|
|
|
"\tparent paths of the first input file . See:\n"
|
2019-07-02 02:55:10 +08:00
|
|
|
"\thttps://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n"
|
2012-07-16 20:46:48 +08:00
|
|
|
"\texample of setting up Clang Tooling on a source tree.\n"
|
|
|
|
"\n"
|
2012-08-23 04:52:52 +08: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-07-16 20:46:48 +08:00
|
|
|
"\n";
|
|
|
|
|
2017-08-19 00:21:08 +08:00
|
|
|
void ArgumentsAdjustingCompilations::appendArgumentsAdjuster(
|
|
|
|
ArgumentsAdjuster Adjuster) {
|
|
|
|
Adjusters.push_back(std::move(Adjuster));
|
|
|
|
}
|
2014-11-04 16:51:24 +08:00
|
|
|
|
2017-08-19 00:21:08 +08:00
|
|
|
std::vector<CompileCommand> ArgumentsAdjustingCompilations::getCompileCommands(
|
|
|
|
StringRef FilePath) const {
|
|
|
|
return adjustCommands(Compilations->getCompileCommands(FilePath));
|
|
|
|
}
|
2014-11-04 16:51:24 +08:00
|
|
|
|
2017-08-19 00:21:08 +08:00
|
|
|
std::vector<std::string>
|
|
|
|
ArgumentsAdjustingCompilations::getAllFiles() const {
|
|
|
|
return Compilations->getAllFiles();
|
|
|
|
}
|
2014-11-04 16:51:24 +08:00
|
|
|
|
2017-08-19 00:21:08 +08:00
|
|
|
std::vector<CompileCommand>
|
|
|
|
ArgumentsAdjustingCompilations::getAllCompileCommands() const {
|
|
|
|
return adjustCommands(Compilations->getAllCompileCommands());
|
|
|
|
}
|
2014-11-04 16:51:24 +08:00
|
|
|
|
2017-08-19 00:21:08 +08:00
|
|
|
std::vector<CompileCommand> ArgumentsAdjustingCompilations::adjustCommands(
|
|
|
|
std::vector<CompileCommand> Commands) const {
|
|
|
|
for (CompileCommand &Command : Commands)
|
|
|
|
for (const auto &Adjuster : Adjusters)
|
|
|
|
Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename);
|
|
|
|
return Commands;
|
|
|
|
}
|
2014-11-04 16:51:24 +08:00
|
|
|
|
2017-10-24 21:10:58 +08:00
|
|
|
llvm::Error CommonOptionsParser::init(
|
2015-08-17 18:01:42 +08:00
|
|
|
int &argc, const char **argv, cl::OptionCategory &Category,
|
|
|
|
llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
|
Filter-out irrelevant command-line options in CommonOptionsParser.
Summary:
Leave only -help, -version and options from the specified category.
Updated clang-check and clang-tidy. As clang-tidy is in a separate repository,
here's the diff:
Index: tools/extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- tools/extra/clang-tidy/tool/ClangTidyMain.cpp (revision 197024)
+++ tools/extra/clang-tidy/tool/ClangTidyMain.cpp (working copy)
@@ -39,7 +39,7 @@
// FIXME: Add option to list name/description of all checks.
int main(int argc, const char **argv) {
- CommonOptionsParser OptionsParser(argc, argv);
+ CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
SmallVector<clang::tidy::ClangTidyError, 16> Errors;
clang::tidy::runClangTidy(Checks, OptionsParser.getCompilations(),
Reviewers: djasper, klimek
Reviewed By: djasper
CC: cfe-commits, revane, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2379
llvm-svn: 197139
2013-12-12 17:59:42 +08:00
|
|
|
|
|
|
|
static cl::opt<std::string> BuildPath("p", cl::desc("Build path"),
|
2017-09-14 21:16:14 +08:00
|
|
|
cl::Optional, cl::cat(Category),
|
|
|
|
cl::sub(*cl::AllSubCommands));
|
2012-08-23 04:52:52 +08:00
|
|
|
|
2012-08-29 06:15:41 +08:00
|
|
|
static cl::list<std::string> SourcePaths(
|
2015-08-17 18:01:42 +08:00
|
|
|
cl::Positional, cl::desc("<source0> [... <sourceN>]"), OccurrencesFlag,
|
2017-09-14 21:16:14 +08:00
|
|
|
cl::cat(Category), cl::sub(*cl::AllSubCommands));
|
Filter-out irrelevant command-line options in CommonOptionsParser.
Summary:
Leave only -help, -version and options from the specified category.
Updated clang-check and clang-tidy. As clang-tidy is in a separate repository,
here's the diff:
Index: tools/extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- tools/extra/clang-tidy/tool/ClangTidyMain.cpp (revision 197024)
+++ tools/extra/clang-tidy/tool/ClangTidyMain.cpp (working copy)
@@ -39,7 +39,7 @@
// FIXME: Add option to list name/description of all checks.
int main(int argc, const char **argv) {
- CommonOptionsParser OptionsParser(argc, argv);
+ CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
SmallVector<clang::tidy::ClangTidyError, 16> Errors;
clang::tidy::runClangTidy(Checks, OptionsParser.getCompilations(),
Reviewers: djasper, klimek
Reviewed By: djasper
CC: cfe-commits, revane, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2379
llvm-svn: 197139
2013-12-12 17:59:42 +08:00
|
|
|
|
2014-11-04 16:51:24 +08:00
|
|
|
static cl::list<std::string> ArgsAfter(
|
|
|
|
"extra-arg",
|
|
|
|
cl::desc("Additional argument to append to the compiler command line"),
|
2017-09-14 21:16:14 +08:00
|
|
|
cl::cat(Category), cl::sub(*cl::AllSubCommands));
|
2014-11-04 16:51:24 +08:00
|
|
|
|
|
|
|
static cl::list<std::string> ArgsBefore(
|
|
|
|
"extra-arg-before",
|
|
|
|
cl::desc("Additional argument to prepend to the compiler command line"),
|
2017-09-14 21:16:14 +08:00
|
|
|
cl::cat(Category), cl::sub(*cl::AllSubCommands));
|
2014-11-04 16:51:24 +08:00
|
|
|
|
2017-10-24 21:10:58 +08:00
|
|
|
cl::ResetAllOptionOccurrences();
|
|
|
|
|
2015-01-22 07:26:11 +08:00
|
|
|
cl::HideUnrelatedOptions(Category);
|
2012-07-16 20:46:48 +08:00
|
|
|
|
2017-05-24 19:57:37 +08:00
|
|
|
std::string ErrorMessage;
|
|
|
|
Compilations =
|
|
|
|
FixedCompilationDatabase::loadFromCommandLine(argc, argv, ErrorMessage);
|
2017-10-24 21:10:58 +08:00
|
|
|
if (!ErrorMessage.empty())
|
|
|
|
ErrorMessage.append("\n");
|
|
|
|
llvm::raw_string_ostream OS(ErrorMessage);
|
|
|
|
// Stop initializing if command-line option parsing failed.
|
|
|
|
if (!cl::ParseCommandLineOptions(argc, argv, Overview, &OS)) {
|
|
|
|
OS.flush();
|
|
|
|
return llvm::make_error<llvm::StringError>("[CommonOptionsParser]: " +
|
|
|
|
ErrorMessage,
|
|
|
|
llvm::inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
|
2016-02-23 18:29:02 +08:00
|
|
|
cl::PrintOptionValues();
|
|
|
|
|
2012-08-23 04:52:52 +08:00
|
|
|
SourcePathList = SourcePaths;
|
2015-08-17 18:01:42 +08:00
|
|
|
if ((OccurrencesFlag == cl::ZeroOrMore || OccurrencesFlag == cl::Optional) &&
|
|
|
|
SourcePathList.empty())
|
2017-10-24 21:10:58 +08:00
|
|
|
return llvm::Error::success();
|
2012-07-16 20:46:48 +08:00
|
|
|
if (!Compilations) {
|
|
|
|
if (!BuildPath.empty()) {
|
2014-08-09 00:06:15 +08:00
|
|
|
Compilations =
|
|
|
|
CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage);
|
2012-07-16 20:46:48 +08:00
|
|
|
} else {
|
2014-08-09 00:06:15 +08:00
|
|
|
Compilations = CompilationDatabase::autoDetectFromSource(SourcePaths[0],
|
|
|
|
ErrorMessage);
|
2012-07-16 20:46:48 +08:00
|
|
|
}
|
2015-12-03 18:38:53 +08:00
|
|
|
if (!Compilations) {
|
|
|
|
llvm::errs() << "Error while trying to load a compilation database:\n"
|
|
|
|
<< ErrorMessage << "Running without flags.\n";
|
|
|
|
Compilations.reset(
|
|
|
|
new FixedCompilationDatabase(".", std::vector<std::string>()));
|
|
|
|
}
|
2012-07-16 20:46:48 +08:00
|
|
|
}
|
2014-11-04 16:51:24 +08:00
|
|
|
auto AdjustingCompilations =
|
2019-08-15 07:04:18 +08:00
|
|
|
std::make_unique<ArgumentsAdjustingCompilations>(
|
2014-11-04 16:51:24 +08:00
|
|
|
std::move(Compilations));
|
2017-10-26 18:38:14 +08:00
|
|
|
Adjuster =
|
|
|
|
getInsertArgumentAdjuster(ArgsBefore, ArgumentInsertPosition::BEGIN);
|
|
|
|
Adjuster = combineAdjusters(
|
|
|
|
std::move(Adjuster),
|
2014-12-04 01:53:02 +08:00
|
|
|
getInsertArgumentAdjuster(ArgsAfter, ArgumentInsertPosition::END));
|
2017-10-26 18:38:14 +08:00
|
|
|
AdjustingCompilations->appendArgumentsAdjuster(Adjuster);
|
2014-11-04 16:51:24 +08:00
|
|
|
Compilations = std::move(AdjustingCompilations);
|
2017-10-24 21:10:58 +08:00
|
|
|
return llvm::Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Expected<CommonOptionsParser> CommonOptionsParser::create(
|
|
|
|
int &argc, const char **argv, llvm::cl::OptionCategory &Category,
|
|
|
|
llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
|
|
|
|
CommonOptionsParser Parser;
|
|
|
|
llvm::Error Err =
|
|
|
|
Parser.init(argc, argv, Category, OccurrencesFlag, Overview);
|
|
|
|
if (Err)
|
|
|
|
return std::move(Err);
|
|
|
|
return std::move(Parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
CommonOptionsParser::CommonOptionsParser(
|
|
|
|
int &argc, const char **argv, cl::OptionCategory &Category,
|
|
|
|
llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
|
|
|
|
llvm::Error Err = init(argc, argv, Category, OccurrencesFlag, Overview);
|
|
|
|
if (Err) {
|
|
|
|
llvm::report_fatal_error(
|
|
|
|
"CommonOptionsParser: failed to parse command-line arguments. " +
|
|
|
|
llvm::toString(std::move(Err)));
|
|
|
|
}
|
2012-07-16 20:46:48 +08:00
|
|
|
}
|