1a75b2cacSAlex Lorenz //===- ClangDiff.cpp - compare source files by AST nodes ------*- C++ -*- -===// 2a75b2cacSAlex Lorenz // 3a75b2cacSAlex Lorenz // The LLVM Compiler Infrastructure 4a75b2cacSAlex Lorenz // 5a75b2cacSAlex Lorenz // This file is distributed under the University of Illinois Open Source 6a75b2cacSAlex Lorenz // License. See LICENSE.TXT for details. 7a75b2cacSAlex Lorenz // 8a75b2cacSAlex Lorenz //===----------------------------------------------------------------------===// 9a75b2cacSAlex Lorenz // 10a75b2cacSAlex Lorenz // This file implements a tool for syntax tree based comparison using 11a75b2cacSAlex Lorenz // Tooling/ASTDiff. 12a75b2cacSAlex Lorenz // 13a75b2cacSAlex Lorenz //===----------------------------------------------------------------------===// 14a75b2cacSAlex Lorenz 15a75b2cacSAlex Lorenz #include "clang/Tooling/ASTDiff/ASTDiff.h" 16a75b2cacSAlex Lorenz #include "clang/Tooling/CommonOptionsParser.h" 17a75b2cacSAlex Lorenz #include "clang/Tooling/Tooling.h" 18a75b2cacSAlex Lorenz #include "llvm/Support/CommandLine.h" 19a75b2cacSAlex Lorenz 20a75b2cacSAlex Lorenz using namespace llvm; 21a75b2cacSAlex Lorenz using namespace clang; 22a75b2cacSAlex Lorenz using namespace clang::tooling; 23a75b2cacSAlex Lorenz 24a75b2cacSAlex Lorenz static cl::OptionCategory ClangDiffCategory("clang-diff options"); 25a75b2cacSAlex Lorenz 26a75b2cacSAlex Lorenz static cl::opt<bool> 27914a958eSJohannes Altmanninger ASTDump("ast-dump", 28a1d2b5d5SJohannes Altmanninger cl::desc("Print the internal representation of the AST."), 29a1d2b5d5SJohannes Altmanninger cl::init(false), cl::cat(ClangDiffCategory)); 30a1d2b5d5SJohannes Altmanninger 31a1d2b5d5SJohannes Altmanninger static cl::opt<bool> ASTDumpJson( 32a1d2b5d5SJohannes Altmanninger "ast-dump-json", 33a75b2cacSAlex Lorenz cl::desc("Print the internal representation of the AST as JSON."), 34a75b2cacSAlex Lorenz cl::init(false), cl::cat(ClangDiffCategory)); 35a75b2cacSAlex Lorenz 36683876caSJohannes Altmanninger static cl::opt<bool> 37683876caSJohannes Altmanninger PrintMatches("dump-matches", cl::desc("Print the matched nodes."), 38683876caSJohannes Altmanninger cl::init(false), cl::cat(ClangDiffCategory)); 39683876caSJohannes Altmanninger 40a29d6aecSJohannes Altmanninger static cl::opt<bool> HtmlDiff("html", 41a29d6aecSJohannes Altmanninger cl::desc("Output a side-by-side diff in HTML."), 42a29d6aecSJohannes Altmanninger cl::init(false), cl::cat(ClangDiffCategory)); 43a29d6aecSJohannes Altmanninger 44a75b2cacSAlex Lorenz static cl::opt<std::string> SourcePath(cl::Positional, cl::desc("<source>"), 45a75b2cacSAlex Lorenz cl::Required, 46a75b2cacSAlex Lorenz cl::cat(ClangDiffCategory)); 47a75b2cacSAlex Lorenz 48a75b2cacSAlex Lorenz static cl::opt<std::string> DestinationPath(cl::Positional, 49a75b2cacSAlex Lorenz cl::desc("<destination>"), 50a75b2cacSAlex Lorenz cl::Optional, 51a75b2cacSAlex Lorenz cl::cat(ClangDiffCategory)); 52a75b2cacSAlex Lorenz 53849f20e4SJohannes Altmanninger static cl::opt<int> MaxSize("s", cl::desc("<maxsize>"), cl::Optional, 54849f20e4SJohannes Altmanninger cl::init(-1), cl::cat(ClangDiffCategory)); 55849f20e4SJohannes Altmanninger 56849f20e4SJohannes Altmanninger static cl::opt<std::string> BuildPath("p", cl::desc("Build path"), cl::init(""), 57849f20e4SJohannes Altmanninger cl::Optional, cl::cat(ClangDiffCategory)); 58849f20e4SJohannes Altmanninger 59849f20e4SJohannes Altmanninger static cl::list<std::string> ArgsAfter( 60849f20e4SJohannes Altmanninger "extra-arg", 61849f20e4SJohannes Altmanninger cl::desc("Additional argument to append to the compiler command line"), 62849f20e4SJohannes Altmanninger cl::cat(ClangDiffCategory)); 63849f20e4SJohannes Altmanninger 64849f20e4SJohannes Altmanninger static cl::list<std::string> ArgsBefore( 65849f20e4SJohannes Altmanninger "extra-arg-before", 66849f20e4SJohannes Altmanninger cl::desc("Additional argument to prepend to the compiler command line"), 67849f20e4SJohannes Altmanninger cl::cat(ClangDiffCategory)); 68849f20e4SJohannes Altmanninger 69849f20e4SJohannes Altmanninger static void addExtraArgs(std::unique_ptr<CompilationDatabase> &Compilations) { 70849f20e4SJohannes Altmanninger if (!Compilations) 71849f20e4SJohannes Altmanninger return; 72849f20e4SJohannes Altmanninger auto AdjustingCompilations = 73849f20e4SJohannes Altmanninger llvm::make_unique<ArgumentsAdjustingCompilations>( 74849f20e4SJohannes Altmanninger std::move(Compilations)); 75849f20e4SJohannes Altmanninger AdjustingCompilations->appendArgumentsAdjuster( 76849f20e4SJohannes Altmanninger getInsertArgumentAdjuster(ArgsBefore, ArgumentInsertPosition::BEGIN)); 77849f20e4SJohannes Altmanninger AdjustingCompilations->appendArgumentsAdjuster( 78849f20e4SJohannes Altmanninger getInsertArgumentAdjuster(ArgsAfter, ArgumentInsertPosition::END)); 79849f20e4SJohannes Altmanninger Compilations = std::move(AdjustingCompilations); 80849f20e4SJohannes Altmanninger } 81849f20e4SJohannes Altmanninger 82849f20e4SJohannes Altmanninger static std::unique_ptr<ASTUnit> 83849f20e4SJohannes Altmanninger getAST(const std::unique_ptr<CompilationDatabase> &CommonCompilations, 84849f20e4SJohannes Altmanninger const StringRef Filename) { 85a75b2cacSAlex Lorenz std::string ErrorMessage; 86a75b2cacSAlex Lorenz std::unique_ptr<CompilationDatabase> Compilations; 87849f20e4SJohannes Altmanninger if (!CommonCompilations) { 88849f20e4SJohannes Altmanninger Compilations = CompilationDatabase::autoDetectFromSource( 89849f20e4SJohannes Altmanninger BuildPath.empty() ? Filename : BuildPath, ErrorMessage); 90a75b2cacSAlex Lorenz if (!Compilations) { 91a75b2cacSAlex Lorenz llvm::errs() 92a75b2cacSAlex Lorenz << "Error while trying to load a compilation database, running " 93a75b2cacSAlex Lorenz "without flags.\n" 94a75b2cacSAlex Lorenz << ErrorMessage; 95849f20e4SJohannes Altmanninger Compilations = 96849f20e4SJohannes Altmanninger llvm::make_unique<clang::tooling::FixedCompilationDatabase>( 97a75b2cacSAlex Lorenz ".", std::vector<std::string>()); 98a75b2cacSAlex Lorenz } 99849f20e4SJohannes Altmanninger } 100849f20e4SJohannes Altmanninger addExtraArgs(Compilations); 101a75b2cacSAlex Lorenz std::array<std::string, 1> Files = {{Filename}}; 102849f20e4SJohannes Altmanninger ClangTool Tool(Compilations ? *Compilations : *CommonCompilations, Files); 103a75b2cacSAlex Lorenz std::vector<std::unique_ptr<ASTUnit>> ASTs; 104a75b2cacSAlex Lorenz Tool.buildASTs(ASTs); 105a75b2cacSAlex Lorenz if (ASTs.size() != Files.size()) 106a75b2cacSAlex Lorenz return nullptr; 107a75b2cacSAlex Lorenz return std::move(ASTs[0]); 108a75b2cacSAlex Lorenz } 109a75b2cacSAlex Lorenz 1100da12c84SJohannes Altmanninger static char hexdigit(int N) { return N &= 0xf, N + (N < 10 ? '0' : 'a' - 10); } 1110da12c84SJohannes Altmanninger 112a29d6aecSJohannes Altmanninger static const char HtmlDiffHeader[] = R"( 113a29d6aecSJohannes Altmanninger <html> 114a29d6aecSJohannes Altmanninger <head> 115a29d6aecSJohannes Altmanninger <meta charset='utf-8'/> 116a29d6aecSJohannes Altmanninger <style> 117a29d6aecSJohannes Altmanninger span.d { color: red; } 118a29d6aecSJohannes Altmanninger span.u { color: #cc00cc; } 119a29d6aecSJohannes Altmanninger span.i { color: green; } 120a29d6aecSJohannes Altmanninger span.m { font-weight: bold; } 121a29d6aecSJohannes Altmanninger span { font-weight: normal; color: black; } 122a29d6aecSJohannes Altmanninger div.code { 123a29d6aecSJohannes Altmanninger width: 48%; 124a29d6aecSJohannes Altmanninger height: 98%; 125a29d6aecSJohannes Altmanninger overflow: scroll; 126a29d6aecSJohannes Altmanninger float: left; 127a29d6aecSJohannes Altmanninger padding: 0 0 0.5% 0.5%; 128a29d6aecSJohannes Altmanninger border: solid 2px LightGrey; 129a29d6aecSJohannes Altmanninger border-radius: 5px; 130a29d6aecSJohannes Altmanninger } 131a29d6aecSJohannes Altmanninger </style> 132a29d6aecSJohannes Altmanninger </head> 133a29d6aecSJohannes Altmanninger <script type='text/javascript'> 134a29d6aecSJohannes Altmanninger highlightStack = [] 135a29d6aecSJohannes Altmanninger function clearHighlight() { 136a29d6aecSJohannes Altmanninger while (highlightStack.length) { 137a29d6aecSJohannes Altmanninger let [l, r] = highlightStack.pop() 138a29d6aecSJohannes Altmanninger document.getElementById(l).style.backgroundColor = 'white' 139a29d6aecSJohannes Altmanninger document.getElementById(r).style.backgroundColor = 'white' 140a29d6aecSJohannes Altmanninger } 141a29d6aecSJohannes Altmanninger } 142a29d6aecSJohannes Altmanninger function highlight(event) { 143a29d6aecSJohannes Altmanninger id = event.target['id'] 144a29d6aecSJohannes Altmanninger doHighlight(id) 145a29d6aecSJohannes Altmanninger } 146a29d6aecSJohannes Altmanninger function doHighlight(id) { 147a29d6aecSJohannes Altmanninger clearHighlight() 148a29d6aecSJohannes Altmanninger source = document.getElementById(id) 149a29d6aecSJohannes Altmanninger if (!source.attributes['tid']) 150a29d6aecSJohannes Altmanninger return 151a29d6aecSJohannes Altmanninger tid = source.attributes['tid'].value 152a29d6aecSJohannes Altmanninger target = document.getElementById(tid) 153a29d6aecSJohannes Altmanninger if (!target || source.parentElement && source.parentElement.classList.contains('code')) 154a29d6aecSJohannes Altmanninger return 155a29d6aecSJohannes Altmanninger source.style.backgroundColor = target.style.backgroundColor = 'lightgrey' 156a29d6aecSJohannes Altmanninger highlightStack.push([id, tid]) 157a29d6aecSJohannes Altmanninger source.scrollIntoView() 158a29d6aecSJohannes Altmanninger target.scrollIntoView() 159a29d6aecSJohannes Altmanninger location.hash = '#' + id 160a29d6aecSJohannes Altmanninger } 161a29d6aecSJohannes Altmanninger function scrollToBoth() { 162a29d6aecSJohannes Altmanninger doHighlight(location.hash.substr(1)) 163a29d6aecSJohannes Altmanninger } 164a29d6aecSJohannes Altmanninger window.onload = scrollToBoth 165a29d6aecSJohannes Altmanninger </script> 166a29d6aecSJohannes Altmanninger <body> 167a29d6aecSJohannes Altmanninger <div onclick='highlight(event)'> 168a29d6aecSJohannes Altmanninger )"; 169a29d6aecSJohannes Altmanninger 170a29d6aecSJohannes Altmanninger static void printHtml(raw_ostream &OS, char C) { 171a29d6aecSJohannes Altmanninger switch (C) { 172a29d6aecSJohannes Altmanninger case '&': 173a29d6aecSJohannes Altmanninger OS << "&"; 174a29d6aecSJohannes Altmanninger break; 175a29d6aecSJohannes Altmanninger case '<': 176a29d6aecSJohannes Altmanninger OS << "<"; 177a29d6aecSJohannes Altmanninger break; 178a29d6aecSJohannes Altmanninger case '>': 179a29d6aecSJohannes Altmanninger OS << ">"; 180a29d6aecSJohannes Altmanninger break; 181a29d6aecSJohannes Altmanninger case '\'': 182a29d6aecSJohannes Altmanninger OS << "'"; 183a29d6aecSJohannes Altmanninger break; 184a29d6aecSJohannes Altmanninger case '"': 185a29d6aecSJohannes Altmanninger OS << """; 186a29d6aecSJohannes Altmanninger break; 187a29d6aecSJohannes Altmanninger default: 188a29d6aecSJohannes Altmanninger OS << C; 189a29d6aecSJohannes Altmanninger } 190a29d6aecSJohannes Altmanninger } 191a29d6aecSJohannes Altmanninger 192a29d6aecSJohannes Altmanninger static void printHtml(raw_ostream &OS, const StringRef Str) { 193a29d6aecSJohannes Altmanninger for (char C : Str) 194a29d6aecSJohannes Altmanninger printHtml(OS, C); 195a29d6aecSJohannes Altmanninger } 196a29d6aecSJohannes Altmanninger 197a29d6aecSJohannes Altmanninger static std::string getChangeKindAbbr(diff::ChangeKind Kind) { 198a29d6aecSJohannes Altmanninger switch (Kind) { 199a29d6aecSJohannes Altmanninger case diff::None: 200a29d6aecSJohannes Altmanninger return ""; 201a29d6aecSJohannes Altmanninger case diff::Delete: 202a29d6aecSJohannes Altmanninger return "d"; 203a29d6aecSJohannes Altmanninger case diff::Update: 204a29d6aecSJohannes Altmanninger return "u"; 205a29d6aecSJohannes Altmanninger case diff::Insert: 206a29d6aecSJohannes Altmanninger return "i"; 207a29d6aecSJohannes Altmanninger case diff::Move: 208a29d6aecSJohannes Altmanninger return "m"; 209a29d6aecSJohannes Altmanninger case diff::UpdateMove: 210a29d6aecSJohannes Altmanninger return "u m"; 211a29d6aecSJohannes Altmanninger } 212*e1a89fbfSJohannes Altmanninger llvm_unreachable("Invalid enumeration value."); 213a29d6aecSJohannes Altmanninger } 214a29d6aecSJohannes Altmanninger 215a29d6aecSJohannes Altmanninger static unsigned printHtmlForNode(raw_ostream &OS, const diff::ASTDiff &Diff, 216a29d6aecSJohannes Altmanninger diff::SyntaxTree &Tree, bool IsLeft, 217a29d6aecSJohannes Altmanninger diff::NodeId Id, unsigned Offset) { 218a29d6aecSJohannes Altmanninger const diff::Node &Node = Tree.getNode(Id); 219a29d6aecSJohannes Altmanninger char MyTag, OtherTag; 220a29d6aecSJohannes Altmanninger diff::NodeId LeftId, RightId; 221a29d6aecSJohannes Altmanninger diff::NodeId TargetId = Diff.getMapped(Tree, Id); 222a29d6aecSJohannes Altmanninger if (IsLeft) { 223a29d6aecSJohannes Altmanninger MyTag = 'L'; 224a29d6aecSJohannes Altmanninger OtherTag = 'R'; 225a29d6aecSJohannes Altmanninger LeftId = Id; 226a29d6aecSJohannes Altmanninger RightId = TargetId; 227a29d6aecSJohannes Altmanninger } else { 228a29d6aecSJohannes Altmanninger MyTag = 'R'; 229a29d6aecSJohannes Altmanninger OtherTag = 'L'; 230a29d6aecSJohannes Altmanninger LeftId = TargetId; 231a29d6aecSJohannes Altmanninger RightId = Id; 232a29d6aecSJohannes Altmanninger } 233a29d6aecSJohannes Altmanninger unsigned Begin, End; 234a29d6aecSJohannes Altmanninger std::tie(Begin, End) = Tree.getSourceRangeOffsets(Node); 235a29d6aecSJohannes Altmanninger const SourceManager &SrcMgr = Tree.getASTContext().getSourceManager(); 236a29d6aecSJohannes Altmanninger auto Code = SrcMgr.getBuffer(SrcMgr.getMainFileID())->getBuffer(); 237a29d6aecSJohannes Altmanninger for (; Offset < Begin; ++Offset) 238a29d6aecSJohannes Altmanninger printHtml(OS, Code[Offset]); 239a29d6aecSJohannes Altmanninger OS << "<span id='" << MyTag << Id << "' " 240a29d6aecSJohannes Altmanninger << "tid='" << OtherTag << TargetId << "' "; 241a29d6aecSJohannes Altmanninger OS << "title='"; 242a29d6aecSJohannes Altmanninger printHtml(OS, Node.getTypeLabel()); 243a29d6aecSJohannes Altmanninger OS << "\n" << LeftId << " -> " << RightId; 244a29d6aecSJohannes Altmanninger std::string Value = Tree.getNodeValue(Node); 245a29d6aecSJohannes Altmanninger if (!Value.empty()) { 246a29d6aecSJohannes Altmanninger OS << "\n"; 247a29d6aecSJohannes Altmanninger printHtml(OS, Value); 248a29d6aecSJohannes Altmanninger } 249a29d6aecSJohannes Altmanninger OS << "'"; 250a29d6aecSJohannes Altmanninger if (Node.Change != diff::None) 251a29d6aecSJohannes Altmanninger OS << " class='" << getChangeKindAbbr(Node.Change) << "'"; 252a29d6aecSJohannes Altmanninger OS << ">"; 253a29d6aecSJohannes Altmanninger 254a29d6aecSJohannes Altmanninger for (diff::NodeId Child : Node.Children) 255a29d6aecSJohannes Altmanninger Offset = printHtmlForNode(OS, Diff, Tree, IsLeft, Child, Offset); 256a29d6aecSJohannes Altmanninger 257a29d6aecSJohannes Altmanninger for (; Offset < End; ++Offset) 258a29d6aecSJohannes Altmanninger printHtml(OS, Code[Offset]); 259a29d6aecSJohannes Altmanninger if (Id == Tree.getRootId()) { 260a29d6aecSJohannes Altmanninger End = Code.size(); 261a29d6aecSJohannes Altmanninger for (; Offset < End; ++Offset) 262a29d6aecSJohannes Altmanninger printHtml(OS, Code[Offset]); 263a29d6aecSJohannes Altmanninger } 264a29d6aecSJohannes Altmanninger OS << "</span>"; 265a29d6aecSJohannes Altmanninger return Offset; 266a29d6aecSJohannes Altmanninger } 267a29d6aecSJohannes Altmanninger 2680da12c84SJohannes Altmanninger static void printJsonString(raw_ostream &OS, const StringRef Str) { 26914be1839SJohannes Altmanninger for (signed char C : Str) { 2700da12c84SJohannes Altmanninger switch (C) { 2710da12c84SJohannes Altmanninger case '"': 2720da12c84SJohannes Altmanninger OS << R"(\")"; 2730da12c84SJohannes Altmanninger break; 2740da12c84SJohannes Altmanninger case '\\': 2750da12c84SJohannes Altmanninger OS << R"(\\)"; 2760da12c84SJohannes Altmanninger break; 2770da12c84SJohannes Altmanninger case '\n': 2780da12c84SJohannes Altmanninger OS << R"(\n)"; 2790da12c84SJohannes Altmanninger break; 2800da12c84SJohannes Altmanninger case '\t': 2810da12c84SJohannes Altmanninger OS << R"(\t)"; 2820da12c84SJohannes Altmanninger break; 2830da12c84SJohannes Altmanninger default: 2840da12c84SJohannes Altmanninger if ('\x00' <= C && C <= '\x1f') { 2850da12c84SJohannes Altmanninger OS << R"(\u00)" << hexdigit(C >> 4) << hexdigit(C); 2860da12c84SJohannes Altmanninger } else { 2870da12c84SJohannes Altmanninger OS << C; 2880da12c84SJohannes Altmanninger } 2890da12c84SJohannes Altmanninger } 2900da12c84SJohannes Altmanninger } 2910da12c84SJohannes Altmanninger } 2920da12c84SJohannes Altmanninger 2930da12c84SJohannes Altmanninger static void printNodeAttributes(raw_ostream &OS, diff::SyntaxTree &Tree, 2940da12c84SJohannes Altmanninger diff::NodeId Id) { 2950da12c84SJohannes Altmanninger const diff::Node &N = Tree.getNode(Id); 2960da12c84SJohannes Altmanninger OS << R"("id":)" << int(Id); 2970da12c84SJohannes Altmanninger OS << R"(,"type":")" << N.getTypeLabel() << '"'; 2980da12c84SJohannes Altmanninger auto Offsets = Tree.getSourceRangeOffsets(N); 2990da12c84SJohannes Altmanninger OS << R"(,"begin":)" << Offsets.first; 3000da12c84SJohannes Altmanninger OS << R"(,"end":)" << Offsets.second; 301e0fe5cd4SJohannes Altmanninger std::string Value = Tree.getNodeValue(N); 3020da12c84SJohannes Altmanninger if (!Value.empty()) { 3030da12c84SJohannes Altmanninger OS << R"(,"value":")"; 3040da12c84SJohannes Altmanninger printJsonString(OS, Value); 3050da12c84SJohannes Altmanninger OS << '"'; 3060da12c84SJohannes Altmanninger } 3070da12c84SJohannes Altmanninger } 3080da12c84SJohannes Altmanninger 3090da12c84SJohannes Altmanninger static void printNodeAsJson(raw_ostream &OS, diff::SyntaxTree &Tree, 3100da12c84SJohannes Altmanninger diff::NodeId Id) { 3110da12c84SJohannes Altmanninger const diff::Node &N = Tree.getNode(Id); 3120da12c84SJohannes Altmanninger OS << "{"; 3130da12c84SJohannes Altmanninger printNodeAttributes(OS, Tree, Id); 3140da12c84SJohannes Altmanninger OS << R"(,"children":[)"; 3150da12c84SJohannes Altmanninger if (N.Children.size() > 0) { 3160da12c84SJohannes Altmanninger printNodeAsJson(OS, Tree, N.Children[0]); 3170da12c84SJohannes Altmanninger for (size_t I = 1, E = N.Children.size(); I < E; ++I) { 3180da12c84SJohannes Altmanninger OS << ","; 3190da12c84SJohannes Altmanninger printNodeAsJson(OS, Tree, N.Children[I]); 3200da12c84SJohannes Altmanninger } 3210da12c84SJohannes Altmanninger } 3220da12c84SJohannes Altmanninger OS << "]}"; 3230da12c84SJohannes Altmanninger } 3240da12c84SJohannes Altmanninger 325e0fe5cd4SJohannes Altmanninger static void printNode(raw_ostream &OS, diff::SyntaxTree &Tree, 326e0fe5cd4SJohannes Altmanninger diff::NodeId Id) { 327e0fe5cd4SJohannes Altmanninger if (Id.isInvalid()) { 328e0fe5cd4SJohannes Altmanninger OS << "None"; 329e0fe5cd4SJohannes Altmanninger return; 330e0fe5cd4SJohannes Altmanninger } 331e0fe5cd4SJohannes Altmanninger OS << Tree.getNode(Id).getTypeLabel(); 332e0fe5cd4SJohannes Altmanninger std::string Value = Tree.getNodeValue(Id); 333e0fe5cd4SJohannes Altmanninger if (!Value.empty()) 334e0fe5cd4SJohannes Altmanninger OS << ": " << Value; 335e0fe5cd4SJohannes Altmanninger OS << "(" << Id << ")"; 336e0fe5cd4SJohannes Altmanninger } 337e0fe5cd4SJohannes Altmanninger 338a1d2b5d5SJohannes Altmanninger static void printTree(raw_ostream &OS, diff::SyntaxTree &Tree) { 339a1d2b5d5SJohannes Altmanninger for (diff::NodeId Id : Tree) { 340a1d2b5d5SJohannes Altmanninger for (int I = 0; I < Tree.getNode(Id).Depth; ++I) 341a1d2b5d5SJohannes Altmanninger OS << " "; 342a1d2b5d5SJohannes Altmanninger printNode(OS, Tree, Id); 343a1d2b5d5SJohannes Altmanninger OS << "\n"; 344a1d2b5d5SJohannes Altmanninger } 345a1d2b5d5SJohannes Altmanninger } 346a1d2b5d5SJohannes Altmanninger 347e0fe5cd4SJohannes Altmanninger static void printDstChange(raw_ostream &OS, diff::ASTDiff &Diff, 348e0fe5cd4SJohannes Altmanninger diff::SyntaxTree &SrcTree, diff::SyntaxTree &DstTree, 349e0fe5cd4SJohannes Altmanninger diff::NodeId Dst) { 350e0fe5cd4SJohannes Altmanninger const diff::Node &DstNode = DstTree.getNode(Dst); 351e0fe5cd4SJohannes Altmanninger diff::NodeId Src = Diff.getMapped(DstTree, Dst); 352e0fe5cd4SJohannes Altmanninger switch (DstNode.Change) { 353e0fe5cd4SJohannes Altmanninger case diff::None: 354e0fe5cd4SJohannes Altmanninger break; 355e0fe5cd4SJohannes Altmanninger case diff::Delete: 356e0fe5cd4SJohannes Altmanninger llvm_unreachable("The destination tree can't have deletions."); 357e0fe5cd4SJohannes Altmanninger case diff::Update: 358e0fe5cd4SJohannes Altmanninger OS << "Update "; 359e0fe5cd4SJohannes Altmanninger printNode(OS, SrcTree, Src); 360e0fe5cd4SJohannes Altmanninger OS << " to " << DstTree.getNodeValue(Dst) << "\n"; 361e0fe5cd4SJohannes Altmanninger break; 362e0fe5cd4SJohannes Altmanninger case diff::Insert: 363e0fe5cd4SJohannes Altmanninger case diff::Move: 364e0fe5cd4SJohannes Altmanninger case diff::UpdateMove: 365e0fe5cd4SJohannes Altmanninger if (DstNode.Change == diff::Insert) 366e0fe5cd4SJohannes Altmanninger OS << "Insert"; 367e0fe5cd4SJohannes Altmanninger else if (DstNode.Change == diff::Move) 368e0fe5cd4SJohannes Altmanninger OS << "Move"; 369e0fe5cd4SJohannes Altmanninger else if (DstNode.Change == diff::UpdateMove) 370e0fe5cd4SJohannes Altmanninger OS << "Update and Move"; 371e0fe5cd4SJohannes Altmanninger OS << " "; 372e0fe5cd4SJohannes Altmanninger printNode(OS, DstTree, Dst); 373e0fe5cd4SJohannes Altmanninger OS << " into "; 374e0fe5cd4SJohannes Altmanninger printNode(OS, DstTree, DstNode.Parent); 375e0fe5cd4SJohannes Altmanninger OS << " at " << DstTree.findPositionInParent(Dst) << "\n"; 376e0fe5cd4SJohannes Altmanninger break; 377e0fe5cd4SJohannes Altmanninger } 378e0fe5cd4SJohannes Altmanninger } 379e0fe5cd4SJohannes Altmanninger 380a75b2cacSAlex Lorenz int main(int argc, const char **argv) { 381849f20e4SJohannes Altmanninger std::string ErrorMessage; 382849f20e4SJohannes Altmanninger std::unique_ptr<CompilationDatabase> CommonCompilations = 383849f20e4SJohannes Altmanninger FixedCompilationDatabase::loadFromCommandLine(argc, argv, ErrorMessage); 384849f20e4SJohannes Altmanninger if (!CommonCompilations && !ErrorMessage.empty()) 385849f20e4SJohannes Altmanninger llvm::errs() << ErrorMessage; 386a75b2cacSAlex Lorenz cl::HideUnrelatedOptions(ClangDiffCategory); 387a75b2cacSAlex Lorenz if (!cl::ParseCommandLineOptions(argc, argv)) { 388a75b2cacSAlex Lorenz cl::PrintOptionValues(); 389a75b2cacSAlex Lorenz return 1; 390a75b2cacSAlex Lorenz } 391a75b2cacSAlex Lorenz 392849f20e4SJohannes Altmanninger addExtraArgs(CommonCompilations); 393849f20e4SJohannes Altmanninger 394a1d2b5d5SJohannes Altmanninger if (ASTDump || ASTDumpJson) { 395a75b2cacSAlex Lorenz if (!DestinationPath.empty()) { 396a75b2cacSAlex Lorenz llvm::errs() << "Error: Please specify exactly one filename.\n"; 397a75b2cacSAlex Lorenz return 1; 398a75b2cacSAlex Lorenz } 399849f20e4SJohannes Altmanninger std::unique_ptr<ASTUnit> AST = getAST(CommonCompilations, SourcePath); 400a75b2cacSAlex Lorenz if (!AST) 401a75b2cacSAlex Lorenz return 1; 402a75b2cacSAlex Lorenz diff::SyntaxTree Tree(AST->getASTContext()); 403a1d2b5d5SJohannes Altmanninger if (ASTDump) { 404a1d2b5d5SJohannes Altmanninger printTree(llvm::outs(), Tree); 405a1d2b5d5SJohannes Altmanninger return 0; 406a1d2b5d5SJohannes Altmanninger } 4070da12c84SJohannes Altmanninger llvm::outs() << R"({"filename":")"; 4080da12c84SJohannes Altmanninger printJsonString(llvm::outs(), SourcePath); 4090da12c84SJohannes Altmanninger llvm::outs() << R"(","root":)"; 4100da12c84SJohannes Altmanninger printNodeAsJson(llvm::outs(), Tree, Tree.getRootId()); 4110da12c84SJohannes Altmanninger llvm::outs() << "}\n"; 412a75b2cacSAlex Lorenz return 0; 413a75b2cacSAlex Lorenz } 414a75b2cacSAlex Lorenz 415a75b2cacSAlex Lorenz if (DestinationPath.empty()) { 416a75b2cacSAlex Lorenz llvm::errs() << "Error: Exactly two paths are required.\n"; 417a75b2cacSAlex Lorenz return 1; 418a75b2cacSAlex Lorenz } 419a75b2cacSAlex Lorenz 420849f20e4SJohannes Altmanninger std::unique_ptr<ASTUnit> Src = getAST(CommonCompilations, SourcePath); 421849f20e4SJohannes Altmanninger std::unique_ptr<ASTUnit> Dst = getAST(CommonCompilations, DestinationPath); 422a75b2cacSAlex Lorenz if (!Src || !Dst) 423a75b2cacSAlex Lorenz return 1; 424a75b2cacSAlex Lorenz 425a75b2cacSAlex Lorenz diff::ComparisonOptions Options; 426849f20e4SJohannes Altmanninger if (MaxSize != -1) 427849f20e4SJohannes Altmanninger Options.MaxSize = MaxSize; 428a75b2cacSAlex Lorenz diff::SyntaxTree SrcTree(Src->getASTContext()); 429a75b2cacSAlex Lorenz diff::SyntaxTree DstTree(Dst->getASTContext()); 430e0fe5cd4SJohannes Altmanninger diff::ASTDiff Diff(SrcTree, DstTree, Options); 431e0fe5cd4SJohannes Altmanninger 432a29d6aecSJohannes Altmanninger if (HtmlDiff) { 433a29d6aecSJohannes Altmanninger llvm::outs() << HtmlDiffHeader << "<pre>"; 434a29d6aecSJohannes Altmanninger llvm::outs() << "<div id='L' class='code'>"; 435a29d6aecSJohannes Altmanninger printHtmlForNode(llvm::outs(), Diff, SrcTree, true, SrcTree.getRootId(), 0); 436a29d6aecSJohannes Altmanninger llvm::outs() << "</div>"; 437a29d6aecSJohannes Altmanninger llvm::outs() << "<div id='R' class='code'>"; 438a29d6aecSJohannes Altmanninger printHtmlForNode(llvm::outs(), Diff, DstTree, false, DstTree.getRootId(), 439a29d6aecSJohannes Altmanninger 0); 440a29d6aecSJohannes Altmanninger llvm::outs() << "</div>"; 441a29d6aecSJohannes Altmanninger llvm::outs() << "</pre></div></body></html>\n"; 442a29d6aecSJohannes Altmanninger return 0; 443a29d6aecSJohannes Altmanninger } 444a29d6aecSJohannes Altmanninger 445e0fe5cd4SJohannes Altmanninger for (diff::NodeId Dst : DstTree) { 446e0fe5cd4SJohannes Altmanninger diff::NodeId Src = Diff.getMapped(DstTree, Dst); 447683876caSJohannes Altmanninger if (PrintMatches && Src.isValid()) { 448e0fe5cd4SJohannes Altmanninger llvm::outs() << "Match "; 449e0fe5cd4SJohannes Altmanninger printNode(llvm::outs(), SrcTree, Src); 450e0fe5cd4SJohannes Altmanninger llvm::outs() << " to "; 451e0fe5cd4SJohannes Altmanninger printNode(llvm::outs(), DstTree, Dst); 452e0fe5cd4SJohannes Altmanninger llvm::outs() << "\n"; 453e0fe5cd4SJohannes Altmanninger } 454e0fe5cd4SJohannes Altmanninger printDstChange(llvm::outs(), Diff, SrcTree, DstTree, Dst); 455e0fe5cd4SJohannes Altmanninger } 456e0fe5cd4SJohannes Altmanninger for (diff::NodeId Src : SrcTree) { 457e0fe5cd4SJohannes Altmanninger if (Diff.getMapped(SrcTree, Src).isInvalid()) { 458e0fe5cd4SJohannes Altmanninger llvm::outs() << "Delete "; 459e0fe5cd4SJohannes Altmanninger printNode(llvm::outs(), SrcTree, Src); 460e0fe5cd4SJohannes Altmanninger llvm::outs() << "\n"; 461e0fe5cd4SJohannes Altmanninger } 462e0fe5cd4SJohannes Altmanninger } 463a75b2cacSAlex Lorenz 464a75b2cacSAlex Lorenz return 0; 465a75b2cacSAlex Lorenz } 466