Skip to content

Commit 849f20e

Browse files
committed
[clang-diff] Add commandline arguments.
Summary: Support command line options for build path and extra arguments This emulates the options accepted by clang tools that use CommonOptionsParser. Add a flag for controlling the maximum size parameter for bottom up matching. Reviewers: arphaman Subscribers: klimek Differential Revision: https://reviews.llvm.org/D36177 llvm-svn: 311173
1 parent fa524d7 commit 849f20e

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
RUN: echo a > %t.cpp
2+
3+
CHECK: unknown type name 'X'
4+
5+
check adding compiler cflags
6+
RUN: clang-diff -ast-dump -extra-arg=-Da=X %t.cpp -- 2>&1 | FileCheck %s
7+
RUN: clang-diff -ast-dump -extra-arg-before=-Da=X %t.cpp -- 2>&1 | FileCheck %s
8+
RUN: clang-diff -ast-dump %t.cpp -- 2>&1 -Da=X | FileCheck %s

clang/test/Tooling/clang-diff-basic.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// RUN: mkdir -p %t
2-
// RUN: %clang_cc1 -E %s > %t/src.cpp
3-
// RUN: %clang_cc1 -E %s > %t/dst.cpp -DDEST
4-
// RUN: clang-diff -no-compilation-database %t/src.cpp %t/dst.cpp | FileCheck %s
1+
// RUN: %clang_cc1 -E %s > %t.src.cpp
2+
// RUN: %clang_cc1 -E %s > %t.dst.cpp -DDEST
3+
// RUN: clang-diff %t.src.cpp %t.dst.cpp -- | FileCheck %s
54

65
#ifndef DEST
76
namespace src {

clang/tools/clang-diff/ClangDiff.cpp

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ static cl::opt<bool>
2828
cl::desc("Print the internal representation of the AST as JSON."),
2929
cl::init(false), cl::cat(ClangDiffCategory));
3030

31-
static cl::opt<bool> NoCompilationDatabase(
32-
"no-compilation-database",
33-
cl::desc(
34-
"Do not attempt to load build settings from a compilation database"),
35-
cl::init(false), cl::cat(ClangDiffCategory));
36-
3731
static cl::opt<std::string> SourcePath(cl::Positional, cl::desc("<source>"),
3832
cl::Required,
3933
cl::cat(ClangDiffCategory));
@@ -43,23 +37,56 @@ static cl::opt<std::string> DestinationPath(cl::Positional,
4337
cl::Optional,
4438
cl::cat(ClangDiffCategory));
4539

46-
static std::unique_ptr<ASTUnit> getAST(const StringRef Filename) {
40+
static cl::opt<int> MaxSize("s", cl::desc("<maxsize>"), cl::Optional,
41+
cl::init(-1), cl::cat(ClangDiffCategory));
42+
43+
static cl::opt<std::string> BuildPath("p", cl::desc("Build path"), cl::init(""),
44+
cl::Optional, cl::cat(ClangDiffCategory));
45+
46+
static cl::list<std::string> ArgsAfter(
47+
"extra-arg",
48+
cl::desc("Additional argument to append to the compiler command line"),
49+
cl::cat(ClangDiffCategory));
50+
51+
static cl::list<std::string> ArgsBefore(
52+
"extra-arg-before",
53+
cl::desc("Additional argument to prepend to the compiler command line"),
54+
cl::cat(ClangDiffCategory));
55+
56+
static void addExtraArgs(std::unique_ptr<CompilationDatabase> &Compilations) {
57+
if (!Compilations)
58+
return;
59+
auto AdjustingCompilations =
60+
llvm::make_unique<ArgumentsAdjustingCompilations>(
61+
std::move(Compilations));
62+
AdjustingCompilations->appendArgumentsAdjuster(
63+
getInsertArgumentAdjuster(ArgsBefore, ArgumentInsertPosition::BEGIN));
64+
AdjustingCompilations->appendArgumentsAdjuster(
65+
getInsertArgumentAdjuster(ArgsAfter, ArgumentInsertPosition::END));
66+
Compilations = std::move(AdjustingCompilations);
67+
}
68+
69+
static std::unique_ptr<ASTUnit>
70+
getAST(const std::unique_ptr<CompilationDatabase> &CommonCompilations,
71+
const StringRef Filename) {
4772
std::string ErrorMessage;
4873
std::unique_ptr<CompilationDatabase> Compilations;
49-
if (!NoCompilationDatabase)
50-
Compilations =
51-
CompilationDatabase::autoDetectFromSource(Filename, ErrorMessage);
52-
if (!Compilations) {
53-
if (!NoCompilationDatabase)
74+
if (!CommonCompilations) {
75+
Compilations = CompilationDatabase::autoDetectFromSource(
76+
BuildPath.empty() ? Filename : BuildPath, ErrorMessage);
77+
if (!Compilations) {
5478
llvm::errs()
5579
<< "Error while trying to load a compilation database, running "
5680
"without flags.\n"
5781
<< ErrorMessage;
58-
Compilations = llvm::make_unique<clang::tooling::FixedCompilationDatabase>(
59-
".", std::vector<std::string>());
82+
Compilations =
83+
llvm::make_unique<clang::tooling::FixedCompilationDatabase>(
84+
".", std::vector<std::string>());
85+
}
6086
}
87+
addExtraArgs(Compilations);
6188
std::array<std::string, 1> Files = {{Filename}};
62-
ClangTool Tool(*Compilations, Files);
89+
ClangTool Tool(Compilations ? *Compilations : *CommonCompilations, Files);
6390
std::vector<std::unique_ptr<ASTUnit>> ASTs;
6491
Tool.buildASTs(ASTs);
6592
if (ASTs.size() != Files.size())
@@ -68,18 +95,25 @@ static std::unique_ptr<ASTUnit> getAST(const StringRef Filename) {
6895
}
6996

7097
int main(int argc, const char **argv) {
98+
std::string ErrorMessage;
99+
std::unique_ptr<CompilationDatabase> CommonCompilations =
100+
FixedCompilationDatabase::loadFromCommandLine(argc, argv, ErrorMessage);
101+
if (!CommonCompilations && !ErrorMessage.empty())
102+
llvm::errs() << ErrorMessage;
71103
cl::HideUnrelatedOptions(ClangDiffCategory);
72104
if (!cl::ParseCommandLineOptions(argc, argv)) {
73105
cl::PrintOptionValues();
74106
return 1;
75107
}
76108

109+
addExtraArgs(CommonCompilations);
110+
77111
if (ASTDump) {
78112
if (!DestinationPath.empty()) {
79113
llvm::errs() << "Error: Please specify exactly one filename.\n";
80114
return 1;
81115
}
82-
std::unique_ptr<ASTUnit> AST = getAST(SourcePath);
116+
std::unique_ptr<ASTUnit> AST = getAST(CommonCompilations, SourcePath);
83117
if (!AST)
84118
return 1;
85119
diff::SyntaxTree Tree(AST->getASTContext());
@@ -92,12 +126,14 @@ int main(int argc, const char **argv) {
92126
return 1;
93127
}
94128

95-
std::unique_ptr<ASTUnit> Src = getAST(SourcePath);
96-
std::unique_ptr<ASTUnit> Dst = getAST(DestinationPath);
129+
std::unique_ptr<ASTUnit> Src = getAST(CommonCompilations, SourcePath);
130+
std::unique_ptr<ASTUnit> Dst = getAST(CommonCompilations, DestinationPath);
97131
if (!Src || !Dst)
98132
return 1;
99133

100134
diff::ComparisonOptions Options;
135+
if (MaxSize != -1)
136+
Options.MaxSize = MaxSize;
101137
diff::SyntaxTree SrcTree(Src->getASTContext());
102138
diff::SyntaxTree DstTree(Dst->getASTContext());
103139
diff::ASTDiff DiffTool(SrcTree, DstTree, Options);

0 commit comments

Comments
 (0)