1 //===- ClangDiff.cpp - compare source files by AST nodes ------*- C++ -*- -===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements a tool for syntax tree based comparison using 11 // Tooling/ASTDiff. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/Tooling/ASTDiff/ASTDiff.h" 16 #include "clang/Tooling/CommonOptionsParser.h" 17 #include "clang/Tooling/Tooling.h" 18 #include "llvm/Support/CommandLine.h" 19 20 using namespace llvm; 21 using namespace clang; 22 using namespace clang::tooling; 23 24 static cl::OptionCategory ClangDiffCategory("clang-diff options"); 25 26 static cl::opt<bool> 27 ASTDump("ast-dump", 28 cl::desc("Print the internal representation of the AST as JSON."), 29 cl::init(false), cl::cat(ClangDiffCategory)); 30 31 static cl::opt<std::string> SourcePath(cl::Positional, cl::desc("<source>"), 32 cl::Required, 33 cl::cat(ClangDiffCategory)); 34 35 static cl::opt<std::string> DestinationPath(cl::Positional, 36 cl::desc("<destination>"), 37 cl::Optional, 38 cl::cat(ClangDiffCategory)); 39 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) { 72 std::string ErrorMessage; 73 std::unique_ptr<CompilationDatabase> Compilations; 74 if (!CommonCompilations) { 75 Compilations = CompilationDatabase::autoDetectFromSource( 76 BuildPath.empty() ? Filename : BuildPath, ErrorMessage); 77 if (!Compilations) { 78 llvm::errs() 79 << "Error while trying to load a compilation database, running " 80 "without flags.\n" 81 << ErrorMessage; 82 Compilations = 83 llvm::make_unique<clang::tooling::FixedCompilationDatabase>( 84 ".", std::vector<std::string>()); 85 } 86 } 87 addExtraArgs(Compilations); 88 std::array<std::string, 1> Files = {{Filename}}; 89 ClangTool Tool(Compilations ? *Compilations : *CommonCompilations, Files); 90 std::vector<std::unique_ptr<ASTUnit>> ASTs; 91 Tool.buildASTs(ASTs); 92 if (ASTs.size() != Files.size()) 93 return nullptr; 94 return std::move(ASTs[0]); 95 } 96 97 static char hexdigit(int N) { return N &= 0xf, N + (N < 10 ? '0' : 'a' - 10); } 98 99 static void printJsonString(raw_ostream &OS, const StringRef Str) { 100 for (char C : Str) { 101 switch (C) { 102 case '"': 103 OS << R"(\")"; 104 break; 105 case '\\': 106 OS << R"(\\)"; 107 break; 108 case '\n': 109 OS << R"(\n)"; 110 break; 111 case '\t': 112 OS << R"(\t)"; 113 break; 114 default: 115 if ('\x00' <= C && C <= '\x1f') { 116 OS << R"(\u00)" << hexdigit(C >> 4) << hexdigit(C); 117 } else { 118 OS << C; 119 } 120 } 121 } 122 } 123 124 static void printNodeAttributes(raw_ostream &OS, diff::SyntaxTree &Tree, 125 diff::NodeId Id) { 126 const diff::Node &N = Tree.getNode(Id); 127 OS << R"("id":)" << int(Id); 128 OS << R"(,"type":")" << N.getTypeLabel() << '"'; 129 auto Offsets = Tree.getSourceRangeOffsets(N); 130 OS << R"(,"begin":)" << Offsets.first; 131 OS << R"(,"end":)" << Offsets.second; 132 std::string Value = Tree.getNodeValue(N.ASTNode); 133 if (!Value.empty()) { 134 OS << R"(,"value":")"; 135 printJsonString(OS, Value); 136 OS << '"'; 137 } 138 } 139 140 static void printNodeAsJson(raw_ostream &OS, diff::SyntaxTree &Tree, 141 diff::NodeId Id) { 142 const diff::Node &N = Tree.getNode(Id); 143 OS << "{"; 144 printNodeAttributes(OS, Tree, Id); 145 OS << R"(,"children":[)"; 146 if (N.Children.size() > 0) { 147 printNodeAsJson(OS, Tree, N.Children[0]); 148 for (size_t I = 1, E = N.Children.size(); I < E; ++I) { 149 OS << ","; 150 printNodeAsJson(OS, Tree, N.Children[I]); 151 } 152 } 153 OS << "]}"; 154 } 155 156 int main(int argc, const char **argv) { 157 std::string ErrorMessage; 158 std::unique_ptr<CompilationDatabase> CommonCompilations = 159 FixedCompilationDatabase::loadFromCommandLine(argc, argv, ErrorMessage); 160 if (!CommonCompilations && !ErrorMessage.empty()) 161 llvm::errs() << ErrorMessage; 162 cl::HideUnrelatedOptions(ClangDiffCategory); 163 if (!cl::ParseCommandLineOptions(argc, argv)) { 164 cl::PrintOptionValues(); 165 return 1; 166 } 167 168 addExtraArgs(CommonCompilations); 169 170 if (ASTDump) { 171 if (!DestinationPath.empty()) { 172 llvm::errs() << "Error: Please specify exactly one filename.\n"; 173 return 1; 174 } 175 std::unique_ptr<ASTUnit> AST = getAST(CommonCompilations, SourcePath); 176 if (!AST) 177 return 1; 178 diff::SyntaxTree Tree(AST->getASTContext()); 179 llvm::outs() << R"({"filename":")"; 180 printJsonString(llvm::outs(), SourcePath); 181 llvm::outs() << R"(","root":)"; 182 printNodeAsJson(llvm::outs(), Tree, Tree.getRootId()); 183 llvm::outs() << "}\n"; 184 return 0; 185 } 186 187 if (DestinationPath.empty()) { 188 llvm::errs() << "Error: Exactly two paths are required.\n"; 189 return 1; 190 } 191 192 std::unique_ptr<ASTUnit> Src = getAST(CommonCompilations, SourcePath); 193 std::unique_ptr<ASTUnit> Dst = getAST(CommonCompilations, DestinationPath); 194 if (!Src || !Dst) 195 return 1; 196 197 diff::ComparisonOptions Options; 198 if (MaxSize != -1) 199 Options.MaxSize = MaxSize; 200 diff::SyntaxTree SrcTree(Src->getASTContext()); 201 diff::SyntaxTree DstTree(Dst->getASTContext()); 202 diff::ASTDiff DiffTool(SrcTree, DstTree, Options); 203 for (const auto &Match : DiffTool.getMatches()) 204 DiffTool.printMatch(llvm::outs(), Match); 205 for (const auto &Change : DiffTool.getChanges()) 206 DiffTool.printChange(llvm::outs(), Change); 207 208 return 0; 209 } 210