xref: /llvm-project/clang/tools/clang-diff/ClangDiff.cpp (revision a584beb9d77f4c2a6600cd262209d7b1093cdb2f)
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 
36*a584beb9SJohannes Altmanninger static cl::opt<bool> PrintMatches("dump-matches",
37*a584beb9SJohannes Altmanninger                                   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 
5346307d73SJacob Gravelle static cl::opt<std::string> StopAfter("stop-diff-after",
54d1969307SJohannes Altmanninger                                       cl::desc("<topdown|bottomup>"),
55d1969307SJohannes Altmanninger                                       cl::Optional, cl::init(""),
56d1969307SJohannes Altmanninger                                       cl::cat(ClangDiffCategory));
57d1969307SJohannes Altmanninger 
58849f20e4SJohannes Altmanninger static cl::opt<int> MaxSize("s", cl::desc("<maxsize>"), cl::Optional,
59849f20e4SJohannes Altmanninger                             cl::init(-1), cl::cat(ClangDiffCategory));
60849f20e4SJohannes Altmanninger 
61849f20e4SJohannes Altmanninger static cl::opt<std::string> BuildPath("p", cl::desc("Build path"), cl::init(""),
62849f20e4SJohannes Altmanninger                                       cl::Optional, cl::cat(ClangDiffCategory));
63849f20e4SJohannes Altmanninger 
64849f20e4SJohannes Altmanninger static cl::list<std::string> ArgsAfter(
65849f20e4SJohannes Altmanninger     "extra-arg",
66849f20e4SJohannes Altmanninger     cl::desc("Additional argument to append to the compiler command line"),
67849f20e4SJohannes Altmanninger     cl::cat(ClangDiffCategory));
68849f20e4SJohannes Altmanninger 
69849f20e4SJohannes Altmanninger static cl::list<std::string> ArgsBefore(
70849f20e4SJohannes Altmanninger     "extra-arg-before",
71849f20e4SJohannes Altmanninger     cl::desc("Additional argument to prepend to the compiler command line"),
72849f20e4SJohannes Altmanninger     cl::cat(ClangDiffCategory));
73849f20e4SJohannes Altmanninger 
74849f20e4SJohannes Altmanninger static void addExtraArgs(std::unique_ptr<CompilationDatabase> &Compilations) {
75849f20e4SJohannes Altmanninger   if (!Compilations)
76849f20e4SJohannes Altmanninger     return;
77849f20e4SJohannes Altmanninger   auto AdjustingCompilations =
78849f20e4SJohannes Altmanninger       llvm::make_unique<ArgumentsAdjustingCompilations>(
79849f20e4SJohannes Altmanninger           std::move(Compilations));
80849f20e4SJohannes Altmanninger   AdjustingCompilations->appendArgumentsAdjuster(
81849f20e4SJohannes Altmanninger       getInsertArgumentAdjuster(ArgsBefore, ArgumentInsertPosition::BEGIN));
82849f20e4SJohannes Altmanninger   AdjustingCompilations->appendArgumentsAdjuster(
83849f20e4SJohannes Altmanninger       getInsertArgumentAdjuster(ArgsAfter, ArgumentInsertPosition::END));
84849f20e4SJohannes Altmanninger   Compilations = std::move(AdjustingCompilations);
85849f20e4SJohannes Altmanninger }
86849f20e4SJohannes Altmanninger 
87849f20e4SJohannes Altmanninger static std::unique_ptr<ASTUnit>
88849f20e4SJohannes Altmanninger getAST(const std::unique_ptr<CompilationDatabase> &CommonCompilations,
89849f20e4SJohannes Altmanninger        const StringRef Filename) {
90a75b2cacSAlex Lorenz   std::string ErrorMessage;
91a75b2cacSAlex Lorenz   std::unique_ptr<CompilationDatabase> Compilations;
92849f20e4SJohannes Altmanninger   if (!CommonCompilations) {
93849f20e4SJohannes Altmanninger     Compilations = CompilationDatabase::autoDetectFromSource(
94849f20e4SJohannes Altmanninger         BuildPath.empty() ? Filename : BuildPath, ErrorMessage);
95a75b2cacSAlex Lorenz     if (!Compilations) {
96a75b2cacSAlex Lorenz       llvm::errs()
97a75b2cacSAlex Lorenz           << "Error while trying to load a compilation database, running "
98a75b2cacSAlex Lorenz              "without flags.\n"
99a75b2cacSAlex Lorenz           << ErrorMessage;
100849f20e4SJohannes Altmanninger       Compilations =
101849f20e4SJohannes Altmanninger           llvm::make_unique<clang::tooling::FixedCompilationDatabase>(
102a75b2cacSAlex Lorenz               ".", std::vector<std::string>());
103a75b2cacSAlex Lorenz     }
104849f20e4SJohannes Altmanninger   }
105849f20e4SJohannes Altmanninger   addExtraArgs(Compilations);
106a75b2cacSAlex Lorenz   std::array<std::string, 1> Files = {{Filename}};
107849f20e4SJohannes Altmanninger   ClangTool Tool(Compilations ? *Compilations : *CommonCompilations, Files);
108a75b2cacSAlex Lorenz   std::vector<std::unique_ptr<ASTUnit>> ASTs;
109a75b2cacSAlex Lorenz   Tool.buildASTs(ASTs);
110a75b2cacSAlex Lorenz   if (ASTs.size() != Files.size())
111a75b2cacSAlex Lorenz     return nullptr;
112a75b2cacSAlex Lorenz   return std::move(ASTs[0]);
113a75b2cacSAlex Lorenz }
114a75b2cacSAlex Lorenz 
1150da12c84SJohannes Altmanninger static char hexdigit(int N) { return N &= 0xf, N + (N < 10 ? '0' : 'a' - 10); }
1160da12c84SJohannes Altmanninger 
117a29d6aecSJohannes Altmanninger static const char HtmlDiffHeader[] = R"(
118a29d6aecSJohannes Altmanninger <html>
119a29d6aecSJohannes Altmanninger <head>
120a29d6aecSJohannes Altmanninger <meta charset='utf-8'/>
121a29d6aecSJohannes Altmanninger <style>
122a29d6aecSJohannes Altmanninger span.d { color: red; }
123a29d6aecSJohannes Altmanninger span.u { color: #cc00cc; }
124a29d6aecSJohannes Altmanninger span.i { color: green; }
125a29d6aecSJohannes Altmanninger span.m { font-weight: bold; }
126a29d6aecSJohannes Altmanninger span   { font-weight: normal; color: black; }
127a29d6aecSJohannes Altmanninger div.code {
128a29d6aecSJohannes Altmanninger   width: 48%;
129a29d6aecSJohannes Altmanninger   height: 98%;
130a29d6aecSJohannes Altmanninger   overflow: scroll;
131a29d6aecSJohannes Altmanninger   float: left;
132a29d6aecSJohannes Altmanninger   padding: 0 0 0.5% 0.5%;
133a29d6aecSJohannes Altmanninger   border: solid 2px LightGrey;
134a29d6aecSJohannes Altmanninger   border-radius: 5px;
135a29d6aecSJohannes Altmanninger }
136a29d6aecSJohannes Altmanninger </style>
137a29d6aecSJohannes Altmanninger </head>
138a29d6aecSJohannes Altmanninger <script type='text/javascript'>
139a29d6aecSJohannes Altmanninger highlightStack = []
140a29d6aecSJohannes Altmanninger function clearHighlight() {
141a29d6aecSJohannes Altmanninger   while (highlightStack.length) {
1428796049fSJohannes Altmanninger     var [l, r] = highlightStack.pop()
14338df0fa6SJohannes Altmanninger     document.getElementById(l).style.backgroundColor = 'inherit'
1448796049fSJohannes Altmanninger     if (r[1] != '-')
14538df0fa6SJohannes Altmanninger       document.getElementById(r).style.backgroundColor = 'inherit'
146a29d6aecSJohannes Altmanninger   }
147a29d6aecSJohannes Altmanninger }
148a29d6aecSJohannes Altmanninger function highlight(event) {
1498796049fSJohannes Altmanninger   var id = event.target['id']
150a29d6aecSJohannes Altmanninger   doHighlight(id)
151a29d6aecSJohannes Altmanninger }
152a29d6aecSJohannes Altmanninger function doHighlight(id) {
153a29d6aecSJohannes Altmanninger   clearHighlight()
154a29d6aecSJohannes Altmanninger   source = document.getElementById(id)
155a29d6aecSJohannes Altmanninger   if (!source.attributes['tid'])
156a29d6aecSJohannes Altmanninger     return
1578796049fSJohannes Altmanninger   var mapped = source
1588796049fSJohannes Altmanninger   while (mapped && mapped.parentElement && mapped.attributes['tid'].value.substr(1) === '-1')
1598796049fSJohannes Altmanninger     mapped = mapped.parentElement
1608796049fSJohannes Altmanninger   var tid = null, target = null
1618796049fSJohannes Altmanninger   if (mapped) {
1628796049fSJohannes Altmanninger     tid = mapped.attributes['tid'].value
163a29d6aecSJohannes Altmanninger     target = document.getElementById(tid)
1648796049fSJohannes Altmanninger   }
1658796049fSJohannes Altmanninger   if (source.parentElement && source.parentElement.classList.contains('code'))
166a29d6aecSJohannes Altmanninger     return
1678796049fSJohannes Altmanninger   source.style.backgroundColor = 'lightgrey'
168a29d6aecSJohannes Altmanninger   source.scrollIntoView()
1698796049fSJohannes Altmanninger   if (target) {
1708796049fSJohannes Altmanninger     if (mapped === source)
1718796049fSJohannes Altmanninger       target.style.backgroundColor = 'lightgrey'
172a29d6aecSJohannes Altmanninger     target.scrollIntoView()
1738796049fSJohannes Altmanninger   }
1748796049fSJohannes Altmanninger   highlightStack.push([id, tid])
175a29d6aecSJohannes Altmanninger   location.hash = '#' + id
176a29d6aecSJohannes Altmanninger }
177a29d6aecSJohannes Altmanninger function scrollToBoth() {
178a29d6aecSJohannes Altmanninger   doHighlight(location.hash.substr(1))
179a29d6aecSJohannes Altmanninger }
1808796049fSJohannes Altmanninger function changed(elem) {
1818796049fSJohannes Altmanninger   return elem.classList.length == 0
1828796049fSJohannes Altmanninger }
1838796049fSJohannes Altmanninger function nextChangedNode(prefix, increment, number) {
1848796049fSJohannes Altmanninger   do {
1858796049fSJohannes Altmanninger     number += increment
1868796049fSJohannes Altmanninger     var elem = document.getElementById(prefix + number)
1878796049fSJohannes Altmanninger   } while(elem && !changed(elem))
1888796049fSJohannes Altmanninger   return elem ? number : null
1898796049fSJohannes Altmanninger }
1908796049fSJohannes Altmanninger function handleKey(e) {
1918796049fSJohannes Altmanninger   var down = e.code === "KeyJ"
1928796049fSJohannes Altmanninger   var up = e.code === "KeyK"
1938796049fSJohannes Altmanninger   if (!down && !up)
1948796049fSJohannes Altmanninger     return
1958796049fSJohannes Altmanninger   var id = highlightStack[0] ? highlightStack[0][0] : 'R0'
1968796049fSJohannes Altmanninger   var oldelem = document.getElementById(id)
1978796049fSJohannes Altmanninger   var number = parseInt(id.substr(1))
1988796049fSJohannes Altmanninger   var increment = down ? 1 : -1
1998796049fSJohannes Altmanninger   var lastnumber = number
2008796049fSJohannes Altmanninger   var prefix = id[0]
2018796049fSJohannes Altmanninger   do {
2028796049fSJohannes Altmanninger     number = nextChangedNode(prefix, increment, number)
2038796049fSJohannes Altmanninger     var elem = document.getElementById(prefix + number)
2048796049fSJohannes Altmanninger     if (up && elem) {
2058796049fSJohannes Altmanninger       while (elem.parentElement && changed(elem.parentElement))
2068796049fSJohannes Altmanninger         elem = elem.parentElement
2078796049fSJohannes Altmanninger       number = elem.id.substr(1)
2088796049fSJohannes Altmanninger     }
2098796049fSJohannes Altmanninger   } while ((down && id !== 'R0' && oldelem.contains(elem)))
2108796049fSJohannes Altmanninger   if (!number)
2118796049fSJohannes Altmanninger     number = lastnumber
2128796049fSJohannes Altmanninger   elem = document.getElementById(prefix + number)
2138796049fSJohannes Altmanninger   doHighlight(prefix + number)
2148796049fSJohannes Altmanninger }
215a29d6aecSJohannes Altmanninger window.onload = scrollToBoth
2168796049fSJohannes Altmanninger window.onkeydown = handleKey
217a29d6aecSJohannes Altmanninger </script>
218a29d6aecSJohannes Altmanninger <body>
219a29d6aecSJohannes Altmanninger <div onclick='highlight(event)'>
220a29d6aecSJohannes Altmanninger )";
221a29d6aecSJohannes Altmanninger 
222a29d6aecSJohannes Altmanninger static void printHtml(raw_ostream &OS, char C) {
223a29d6aecSJohannes Altmanninger   switch (C) {
224a29d6aecSJohannes Altmanninger   case '&':
225a29d6aecSJohannes Altmanninger     OS << "&amp;";
226a29d6aecSJohannes Altmanninger     break;
227a29d6aecSJohannes Altmanninger   case '<':
228a29d6aecSJohannes Altmanninger     OS << "&lt;";
229a29d6aecSJohannes Altmanninger     break;
230a29d6aecSJohannes Altmanninger   case '>':
231a29d6aecSJohannes Altmanninger     OS << "&gt;";
232a29d6aecSJohannes Altmanninger     break;
233a29d6aecSJohannes Altmanninger   case '\'':
234a29d6aecSJohannes Altmanninger     OS << "&#x27;";
235a29d6aecSJohannes Altmanninger     break;
236a29d6aecSJohannes Altmanninger   case '"':
237a29d6aecSJohannes Altmanninger     OS << "&quot;";
238a29d6aecSJohannes Altmanninger     break;
239a29d6aecSJohannes Altmanninger   default:
240a29d6aecSJohannes Altmanninger     OS << C;
241a29d6aecSJohannes Altmanninger   }
242a29d6aecSJohannes Altmanninger }
243a29d6aecSJohannes Altmanninger 
244a29d6aecSJohannes Altmanninger static void printHtml(raw_ostream &OS, const StringRef Str) {
245a29d6aecSJohannes Altmanninger   for (char C : Str)
246a29d6aecSJohannes Altmanninger     printHtml(OS, C);
247a29d6aecSJohannes Altmanninger }
248a29d6aecSJohannes Altmanninger 
249a29d6aecSJohannes Altmanninger static std::string getChangeKindAbbr(diff::ChangeKind Kind) {
250a29d6aecSJohannes Altmanninger   switch (Kind) {
251a29d6aecSJohannes Altmanninger   case diff::None:
252a29d6aecSJohannes Altmanninger     return "";
253a29d6aecSJohannes Altmanninger   case diff::Delete:
254a29d6aecSJohannes Altmanninger     return "d";
255a29d6aecSJohannes Altmanninger   case diff::Update:
256a29d6aecSJohannes Altmanninger     return "u";
257a29d6aecSJohannes Altmanninger   case diff::Insert:
258a29d6aecSJohannes Altmanninger     return "i";
259a29d6aecSJohannes Altmanninger   case diff::Move:
260a29d6aecSJohannes Altmanninger     return "m";
261a29d6aecSJohannes Altmanninger   case diff::UpdateMove:
262a29d6aecSJohannes Altmanninger     return "u m";
263a29d6aecSJohannes Altmanninger   }
264e1a89fbfSJohannes Altmanninger   llvm_unreachable("Invalid enumeration value.");
265a29d6aecSJohannes Altmanninger }
266a29d6aecSJohannes Altmanninger 
267a29d6aecSJohannes Altmanninger static unsigned printHtmlForNode(raw_ostream &OS, const diff::ASTDiff &Diff,
268a29d6aecSJohannes Altmanninger                                  diff::SyntaxTree &Tree, bool IsLeft,
269a29d6aecSJohannes Altmanninger                                  diff::NodeId Id, unsigned Offset) {
270a29d6aecSJohannes Altmanninger   const diff::Node &Node = Tree.getNode(Id);
271a29d6aecSJohannes Altmanninger   char MyTag, OtherTag;
272a29d6aecSJohannes Altmanninger   diff::NodeId LeftId, RightId;
273a29d6aecSJohannes Altmanninger   diff::NodeId TargetId = Diff.getMapped(Tree, Id);
274a29d6aecSJohannes Altmanninger   if (IsLeft) {
275a29d6aecSJohannes Altmanninger     MyTag = 'L';
276a29d6aecSJohannes Altmanninger     OtherTag = 'R';
277a29d6aecSJohannes Altmanninger     LeftId = Id;
278a29d6aecSJohannes Altmanninger     RightId = TargetId;
279a29d6aecSJohannes Altmanninger   } else {
280a29d6aecSJohannes Altmanninger     MyTag = 'R';
281a29d6aecSJohannes Altmanninger     OtherTag = 'L';
282a29d6aecSJohannes Altmanninger     LeftId = TargetId;
283a29d6aecSJohannes Altmanninger     RightId = Id;
284a29d6aecSJohannes Altmanninger   }
285a29d6aecSJohannes Altmanninger   unsigned Begin, End;
286a29d6aecSJohannes Altmanninger   std::tie(Begin, End) = Tree.getSourceRangeOffsets(Node);
287a29d6aecSJohannes Altmanninger   const SourceManager &SrcMgr = Tree.getASTContext().getSourceManager();
288a29d6aecSJohannes Altmanninger   auto Code = SrcMgr.getBuffer(SrcMgr.getMainFileID())->getBuffer();
289a29d6aecSJohannes Altmanninger   for (; Offset < Begin; ++Offset)
290a29d6aecSJohannes Altmanninger     printHtml(OS, Code[Offset]);
291a29d6aecSJohannes Altmanninger   OS << "<span id='" << MyTag << Id << "' "
292a29d6aecSJohannes Altmanninger      << "tid='" << OtherTag << TargetId << "' ";
293a29d6aecSJohannes Altmanninger   OS << "title='";
294a29d6aecSJohannes Altmanninger   printHtml(OS, Node.getTypeLabel());
295a29d6aecSJohannes Altmanninger   OS << "\n" << LeftId << " -> " << RightId;
296a29d6aecSJohannes Altmanninger   std::string Value = Tree.getNodeValue(Node);
297a29d6aecSJohannes Altmanninger   if (!Value.empty()) {
298a29d6aecSJohannes Altmanninger     OS << "\n";
299a29d6aecSJohannes Altmanninger     printHtml(OS, Value);
300a29d6aecSJohannes Altmanninger   }
301a29d6aecSJohannes Altmanninger   OS << "'";
302a29d6aecSJohannes Altmanninger   if (Node.Change != diff::None)
303a29d6aecSJohannes Altmanninger     OS << " class='" << getChangeKindAbbr(Node.Change) << "'";
304a29d6aecSJohannes Altmanninger   OS << ">";
305a29d6aecSJohannes Altmanninger 
306a29d6aecSJohannes Altmanninger   for (diff::NodeId Child : Node.Children)
307a29d6aecSJohannes Altmanninger     Offset = printHtmlForNode(OS, Diff, Tree, IsLeft, Child, Offset);
308a29d6aecSJohannes Altmanninger 
309a29d6aecSJohannes Altmanninger   for (; Offset < End; ++Offset)
310a29d6aecSJohannes Altmanninger     printHtml(OS, Code[Offset]);
311a29d6aecSJohannes Altmanninger   if (Id == Tree.getRootId()) {
312a29d6aecSJohannes Altmanninger     End = Code.size();
313a29d6aecSJohannes Altmanninger     for (; Offset < End; ++Offset)
314a29d6aecSJohannes Altmanninger       printHtml(OS, Code[Offset]);
315a29d6aecSJohannes Altmanninger   }
316a29d6aecSJohannes Altmanninger   OS << "</span>";
317a29d6aecSJohannes Altmanninger   return Offset;
318a29d6aecSJohannes Altmanninger }
319a29d6aecSJohannes Altmanninger 
3200da12c84SJohannes Altmanninger static void printJsonString(raw_ostream &OS, const StringRef Str) {
32114be1839SJohannes Altmanninger   for (signed char C : Str) {
3220da12c84SJohannes Altmanninger     switch (C) {
3230da12c84SJohannes Altmanninger     case '"':
3240da12c84SJohannes Altmanninger       OS << R"(\")";
3250da12c84SJohannes Altmanninger       break;
3260da12c84SJohannes Altmanninger     case '\\':
3270da12c84SJohannes Altmanninger       OS << R"(\\)";
3280da12c84SJohannes Altmanninger       break;
3290da12c84SJohannes Altmanninger     case '\n':
3300da12c84SJohannes Altmanninger       OS << R"(\n)";
3310da12c84SJohannes Altmanninger       break;
3320da12c84SJohannes Altmanninger     case '\t':
3330da12c84SJohannes Altmanninger       OS << R"(\t)";
3340da12c84SJohannes Altmanninger       break;
3350da12c84SJohannes Altmanninger     default:
3360da12c84SJohannes Altmanninger       if ('\x00' <= C && C <= '\x1f') {
3370da12c84SJohannes Altmanninger         OS << R"(\u00)" << hexdigit(C >> 4) << hexdigit(C);
3380da12c84SJohannes Altmanninger       } else {
3390da12c84SJohannes Altmanninger         OS << C;
3400da12c84SJohannes Altmanninger       }
3410da12c84SJohannes Altmanninger     }
3420da12c84SJohannes Altmanninger   }
3430da12c84SJohannes Altmanninger }
3440da12c84SJohannes Altmanninger 
3450da12c84SJohannes Altmanninger static void printNodeAttributes(raw_ostream &OS, diff::SyntaxTree &Tree,
3460da12c84SJohannes Altmanninger                                 diff::NodeId Id) {
3470da12c84SJohannes Altmanninger   const diff::Node &N = Tree.getNode(Id);
3480da12c84SJohannes Altmanninger   OS << R"("id":)" << int(Id);
3490da12c84SJohannes Altmanninger   OS << R"(,"type":")" << N.getTypeLabel() << '"';
3500da12c84SJohannes Altmanninger   auto Offsets = Tree.getSourceRangeOffsets(N);
3510da12c84SJohannes Altmanninger   OS << R"(,"begin":)" << Offsets.first;
3520da12c84SJohannes Altmanninger   OS << R"(,"end":)" << Offsets.second;
353e0fe5cd4SJohannes Altmanninger   std::string Value = Tree.getNodeValue(N);
3540da12c84SJohannes Altmanninger   if (!Value.empty()) {
3550da12c84SJohannes Altmanninger     OS << R"(,"value":")";
3560da12c84SJohannes Altmanninger     printJsonString(OS, Value);
3570da12c84SJohannes Altmanninger     OS << '"';
3580da12c84SJohannes Altmanninger   }
3590da12c84SJohannes Altmanninger }
3600da12c84SJohannes Altmanninger 
3610da12c84SJohannes Altmanninger static void printNodeAsJson(raw_ostream &OS, diff::SyntaxTree &Tree,
3620da12c84SJohannes Altmanninger                             diff::NodeId Id) {
3630da12c84SJohannes Altmanninger   const diff::Node &N = Tree.getNode(Id);
3640da12c84SJohannes Altmanninger   OS << "{";
3650da12c84SJohannes Altmanninger   printNodeAttributes(OS, Tree, Id);
3660dd86dc5SJohannes Altmanninger   auto Identifier = N.getIdentifier();
3670dd86dc5SJohannes Altmanninger   auto QualifiedIdentifier = N.getQualifiedIdentifier();
3680dd86dc5SJohannes Altmanninger   if (Identifier) {
3690dd86dc5SJohannes Altmanninger     OS << R"(,"identifier":")";
3700dd86dc5SJohannes Altmanninger     printJsonString(OS, *Identifier);
3710dd86dc5SJohannes Altmanninger     OS << R"(")";
3720dd86dc5SJohannes Altmanninger     if (QualifiedIdentifier && *Identifier != *QualifiedIdentifier) {
3730dd86dc5SJohannes Altmanninger       OS << R"(,"qualified_identifier":")";
3740dd86dc5SJohannes Altmanninger       printJsonString(OS, *QualifiedIdentifier);
3750dd86dc5SJohannes Altmanninger       OS << R"(")";
3760dd86dc5SJohannes Altmanninger     }
3770dd86dc5SJohannes Altmanninger   }
3780da12c84SJohannes Altmanninger   OS << R"(,"children":[)";
3790da12c84SJohannes Altmanninger   if (N.Children.size() > 0) {
3800da12c84SJohannes Altmanninger     printNodeAsJson(OS, Tree, N.Children[0]);
3810da12c84SJohannes Altmanninger     for (size_t I = 1, E = N.Children.size(); I < E; ++I) {
3820da12c84SJohannes Altmanninger       OS << ",";
3830da12c84SJohannes Altmanninger       printNodeAsJson(OS, Tree, N.Children[I]);
3840da12c84SJohannes Altmanninger     }
3850da12c84SJohannes Altmanninger   }
3860da12c84SJohannes Altmanninger   OS << "]}";
3870da12c84SJohannes Altmanninger }
3880da12c84SJohannes Altmanninger 
389e0fe5cd4SJohannes Altmanninger static void printNode(raw_ostream &OS, diff::SyntaxTree &Tree,
390e0fe5cd4SJohannes Altmanninger                       diff::NodeId Id) {
391e0fe5cd4SJohannes Altmanninger   if (Id.isInvalid()) {
392e0fe5cd4SJohannes Altmanninger     OS << "None";
393e0fe5cd4SJohannes Altmanninger     return;
394e0fe5cd4SJohannes Altmanninger   }
395e0fe5cd4SJohannes Altmanninger   OS << Tree.getNode(Id).getTypeLabel();
396e0fe5cd4SJohannes Altmanninger   std::string Value = Tree.getNodeValue(Id);
397e0fe5cd4SJohannes Altmanninger   if (!Value.empty())
398e0fe5cd4SJohannes Altmanninger     OS << ": " << Value;
399e0fe5cd4SJohannes Altmanninger   OS << "(" << Id << ")";
400e0fe5cd4SJohannes Altmanninger }
401e0fe5cd4SJohannes Altmanninger 
402a1d2b5d5SJohannes Altmanninger static void printTree(raw_ostream &OS, diff::SyntaxTree &Tree) {
403a1d2b5d5SJohannes Altmanninger   for (diff::NodeId Id : Tree) {
404a1d2b5d5SJohannes Altmanninger     for (int I = 0; I < Tree.getNode(Id).Depth; ++I)
405a1d2b5d5SJohannes Altmanninger       OS << " ";
406a1d2b5d5SJohannes Altmanninger     printNode(OS, Tree, Id);
407a1d2b5d5SJohannes Altmanninger     OS << "\n";
408a1d2b5d5SJohannes Altmanninger   }
409a1d2b5d5SJohannes Altmanninger }
410a1d2b5d5SJohannes Altmanninger 
411e0fe5cd4SJohannes Altmanninger static void printDstChange(raw_ostream &OS, diff::ASTDiff &Diff,
412e0fe5cd4SJohannes Altmanninger                            diff::SyntaxTree &SrcTree, diff::SyntaxTree &DstTree,
413e0fe5cd4SJohannes Altmanninger                            diff::NodeId Dst) {
414e0fe5cd4SJohannes Altmanninger   const diff::Node &DstNode = DstTree.getNode(Dst);
415e0fe5cd4SJohannes Altmanninger   diff::NodeId Src = Diff.getMapped(DstTree, Dst);
416e0fe5cd4SJohannes Altmanninger   switch (DstNode.Change) {
417e0fe5cd4SJohannes Altmanninger   case diff::None:
418e0fe5cd4SJohannes Altmanninger     break;
419e0fe5cd4SJohannes Altmanninger   case diff::Delete:
420e0fe5cd4SJohannes Altmanninger     llvm_unreachable("The destination tree can't have deletions.");
421e0fe5cd4SJohannes Altmanninger   case diff::Update:
422e0fe5cd4SJohannes Altmanninger     OS << "Update ";
423e0fe5cd4SJohannes Altmanninger     printNode(OS, SrcTree, Src);
424e0fe5cd4SJohannes Altmanninger     OS << " to " << DstTree.getNodeValue(Dst) << "\n";
425e0fe5cd4SJohannes Altmanninger     break;
426e0fe5cd4SJohannes Altmanninger   case diff::Insert:
427e0fe5cd4SJohannes Altmanninger   case diff::Move:
428e0fe5cd4SJohannes Altmanninger   case diff::UpdateMove:
429e0fe5cd4SJohannes Altmanninger     if (DstNode.Change == diff::Insert)
430e0fe5cd4SJohannes Altmanninger       OS << "Insert";
431e0fe5cd4SJohannes Altmanninger     else if (DstNode.Change == diff::Move)
432e0fe5cd4SJohannes Altmanninger       OS << "Move";
433e0fe5cd4SJohannes Altmanninger     else if (DstNode.Change == diff::UpdateMove)
434e0fe5cd4SJohannes Altmanninger       OS << "Update and Move";
435e0fe5cd4SJohannes Altmanninger     OS << " ";
436e0fe5cd4SJohannes Altmanninger     printNode(OS, DstTree, Dst);
437e0fe5cd4SJohannes Altmanninger     OS << " into ";
438e0fe5cd4SJohannes Altmanninger     printNode(OS, DstTree, DstNode.Parent);
439e0fe5cd4SJohannes Altmanninger     OS << " at " << DstTree.findPositionInParent(Dst) << "\n";
440e0fe5cd4SJohannes Altmanninger     break;
441e0fe5cd4SJohannes Altmanninger   }
442e0fe5cd4SJohannes Altmanninger }
443e0fe5cd4SJohannes Altmanninger 
444a75b2cacSAlex Lorenz int main(int argc, const char **argv) {
445849f20e4SJohannes Altmanninger   std::string ErrorMessage;
446849f20e4SJohannes Altmanninger   std::unique_ptr<CompilationDatabase> CommonCompilations =
447849f20e4SJohannes Altmanninger       FixedCompilationDatabase::loadFromCommandLine(argc, argv, ErrorMessage);
448849f20e4SJohannes Altmanninger   if (!CommonCompilations && !ErrorMessage.empty())
449849f20e4SJohannes Altmanninger     llvm::errs() << ErrorMessage;
450a75b2cacSAlex Lorenz   cl::HideUnrelatedOptions(ClangDiffCategory);
451a75b2cacSAlex Lorenz   if (!cl::ParseCommandLineOptions(argc, argv)) {
452a75b2cacSAlex Lorenz     cl::PrintOptionValues();
453a75b2cacSAlex Lorenz     return 1;
454a75b2cacSAlex Lorenz   }
455a75b2cacSAlex Lorenz 
456849f20e4SJohannes Altmanninger   addExtraArgs(CommonCompilations);
457849f20e4SJohannes Altmanninger 
458a1d2b5d5SJohannes Altmanninger   if (ASTDump || ASTDumpJson) {
459a75b2cacSAlex Lorenz     if (!DestinationPath.empty()) {
460a75b2cacSAlex Lorenz       llvm::errs() << "Error: Please specify exactly one filename.\n";
461a75b2cacSAlex Lorenz       return 1;
462a75b2cacSAlex Lorenz     }
463849f20e4SJohannes Altmanninger     std::unique_ptr<ASTUnit> AST = getAST(CommonCompilations, SourcePath);
464a75b2cacSAlex Lorenz     if (!AST)
465a75b2cacSAlex Lorenz       return 1;
466a75b2cacSAlex Lorenz     diff::SyntaxTree Tree(AST->getASTContext());
467a1d2b5d5SJohannes Altmanninger     if (ASTDump) {
468a1d2b5d5SJohannes Altmanninger       printTree(llvm::outs(), Tree);
469a1d2b5d5SJohannes Altmanninger       return 0;
470a1d2b5d5SJohannes Altmanninger     }
4710da12c84SJohannes Altmanninger     llvm::outs() << R"({"filename":")";
4720da12c84SJohannes Altmanninger     printJsonString(llvm::outs(), SourcePath);
4730da12c84SJohannes Altmanninger     llvm::outs() << R"(","root":)";
4740da12c84SJohannes Altmanninger     printNodeAsJson(llvm::outs(), Tree, Tree.getRootId());
4750da12c84SJohannes Altmanninger     llvm::outs() << "}\n";
476a75b2cacSAlex Lorenz     return 0;
477a75b2cacSAlex Lorenz   }
478a75b2cacSAlex Lorenz 
479a75b2cacSAlex Lorenz   if (DestinationPath.empty()) {
480a75b2cacSAlex Lorenz     llvm::errs() << "Error: Exactly two paths are required.\n";
481a75b2cacSAlex Lorenz     return 1;
482a75b2cacSAlex Lorenz   }
483a75b2cacSAlex Lorenz 
484849f20e4SJohannes Altmanninger   std::unique_ptr<ASTUnit> Src = getAST(CommonCompilations, SourcePath);
485849f20e4SJohannes Altmanninger   std::unique_ptr<ASTUnit> Dst = getAST(CommonCompilations, DestinationPath);
486a75b2cacSAlex Lorenz   if (!Src || !Dst)
487a75b2cacSAlex Lorenz     return 1;
488a75b2cacSAlex Lorenz 
489a75b2cacSAlex Lorenz   diff::ComparisonOptions Options;
490849f20e4SJohannes Altmanninger   if (MaxSize != -1)
491849f20e4SJohannes Altmanninger     Options.MaxSize = MaxSize;
492d1969307SJohannes Altmanninger   if (!StopAfter.empty()) {
493d1969307SJohannes Altmanninger     if (StopAfter == "topdown")
494d1969307SJohannes Altmanninger       Options.StopAfterTopDown = true;
495d1969307SJohannes Altmanninger     else if (StopAfter != "bottomup") {
496d1969307SJohannes Altmanninger       llvm::errs() << "Error: Invalid argument for -stop-after\n";
497d1969307SJohannes Altmanninger       return 1;
498d1969307SJohannes Altmanninger     }
499d1969307SJohannes Altmanninger   }
500a75b2cacSAlex Lorenz   diff::SyntaxTree SrcTree(Src->getASTContext());
501a75b2cacSAlex Lorenz   diff::SyntaxTree DstTree(Dst->getASTContext());
502e0fe5cd4SJohannes Altmanninger   diff::ASTDiff Diff(SrcTree, DstTree, Options);
503e0fe5cd4SJohannes Altmanninger 
504a29d6aecSJohannes Altmanninger   if (HtmlDiff) {
505a29d6aecSJohannes Altmanninger     llvm::outs() << HtmlDiffHeader << "<pre>";
506a29d6aecSJohannes Altmanninger     llvm::outs() << "<div id='L' class='code'>";
507a29d6aecSJohannes Altmanninger     printHtmlForNode(llvm::outs(), Diff, SrcTree, true, SrcTree.getRootId(), 0);
508a29d6aecSJohannes Altmanninger     llvm::outs() << "</div>";
509a29d6aecSJohannes Altmanninger     llvm::outs() << "<div id='R' class='code'>";
510a29d6aecSJohannes Altmanninger     printHtmlForNode(llvm::outs(), Diff, DstTree, false, DstTree.getRootId(),
511a29d6aecSJohannes Altmanninger                      0);
512a29d6aecSJohannes Altmanninger     llvm::outs() << "</div>";
513a29d6aecSJohannes Altmanninger     llvm::outs() << "</pre></div></body></html>\n";
514a29d6aecSJohannes Altmanninger     return 0;
515a29d6aecSJohannes Altmanninger   }
516a29d6aecSJohannes Altmanninger 
517e0fe5cd4SJohannes Altmanninger   for (diff::NodeId Dst : DstTree) {
518e0fe5cd4SJohannes Altmanninger     diff::NodeId Src = Diff.getMapped(DstTree, Dst);
519683876caSJohannes Altmanninger     if (PrintMatches && Src.isValid()) {
520e0fe5cd4SJohannes Altmanninger       llvm::outs() << "Match ";
521e0fe5cd4SJohannes Altmanninger       printNode(llvm::outs(), SrcTree, Src);
522e0fe5cd4SJohannes Altmanninger       llvm::outs() << " to ";
523e0fe5cd4SJohannes Altmanninger       printNode(llvm::outs(), DstTree, Dst);
524e0fe5cd4SJohannes Altmanninger       llvm::outs() << "\n";
525e0fe5cd4SJohannes Altmanninger     }
526e0fe5cd4SJohannes Altmanninger     printDstChange(llvm::outs(), Diff, SrcTree, DstTree, Dst);
527e0fe5cd4SJohannes Altmanninger   }
528e0fe5cd4SJohannes Altmanninger   for (diff::NodeId Src : SrcTree) {
529e0fe5cd4SJohannes Altmanninger     if (Diff.getMapped(SrcTree, Src).isInvalid()) {
530e0fe5cd4SJohannes Altmanninger       llvm::outs() << "Delete ";
531e0fe5cd4SJohannes Altmanninger       printNode(llvm::outs(), SrcTree, Src);
532e0fe5cd4SJohannes Altmanninger       llvm::outs() << "\n";
533e0fe5cd4SJohannes Altmanninger     }
534e0fe5cd4SJohannes Altmanninger   }
535a75b2cacSAlex Lorenz 
536a75b2cacSAlex Lorenz   return 0;
537a75b2cacSAlex Lorenz }
538