@@ -28,12 +28,6 @@ static cl::opt<bool>
28
28
cl::desc (" Print the internal representation of the AST as JSON." ),
29
29
cl::init(false ), cl::cat(ClangDiffCategory));
30
30
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
-
37
31
static cl::opt<std::string> SourcePath (cl::Positional, cl::desc(" <source>" ),
38
32
cl::Required,
39
33
cl::cat(ClangDiffCategory));
@@ -43,23 +37,56 @@ static cl::opt<std::string> DestinationPath(cl::Positional,
43
37
cl::Optional,
44
38
cl::cat(ClangDiffCategory));
45
39
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) {
47
72
std::string ErrorMessage;
48
73
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) {
54
78
llvm::errs ()
55
79
<< " Error while trying to load a compilation database, running "
56
80
" without flags.\n "
57
81
<< 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
+ }
60
86
}
87
+ addExtraArgs (Compilations);
61
88
std::array<std::string, 1 > Files = {{Filename}};
62
- ClangTool Tool (*Compilations, Files);
89
+ ClangTool Tool (Compilations ? *Compilations : *CommonCompilations , Files);
63
90
std::vector<std::unique_ptr<ASTUnit>> ASTs;
64
91
Tool.buildASTs (ASTs);
65
92
if (ASTs.size () != Files.size ())
@@ -68,18 +95,25 @@ static std::unique_ptr<ASTUnit> getAST(const StringRef Filename) {
68
95
}
69
96
70
97
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;
71
103
cl::HideUnrelatedOptions (ClangDiffCategory);
72
104
if (!cl::ParseCommandLineOptions (argc, argv)) {
73
105
cl::PrintOptionValues ();
74
106
return 1 ;
75
107
}
76
108
109
+ addExtraArgs (CommonCompilations);
110
+
77
111
if (ASTDump) {
78
112
if (!DestinationPath.empty ()) {
79
113
llvm::errs () << " Error: Please specify exactly one filename.\n " ;
80
114
return 1 ;
81
115
}
82
- std::unique_ptr<ASTUnit> AST = getAST (SourcePath);
116
+ std::unique_ptr<ASTUnit> AST = getAST (CommonCompilations, SourcePath);
83
117
if (!AST)
84
118
return 1 ;
85
119
diff::SyntaxTree Tree (AST->getASTContext ());
@@ -92,12 +126,14 @@ int main(int argc, const char **argv) {
92
126
return 1 ;
93
127
}
94
128
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);
97
131
if (!Src || !Dst)
98
132
return 1 ;
99
133
100
134
diff::ComparisonOptions Options;
135
+ if (MaxSize != -1 )
136
+ Options.MaxSize = MaxSize;
101
137
diff::SyntaxTree SrcTree (Src->getASTContext ());
102
138
diff::SyntaxTree DstTree (Dst->getASTContext ());
103
139
diff::ASTDiff DiffTool (SrcTree, DstTree, Options);
0 commit comments