aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/tools/astdiff/astdiff.cpp
blob: f35b04bbf340c5af137747f43e5abe4421e1cf56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <yql/essentials/utils/backtrace/backtrace.h>

#include <yql/essentials/ast/yql_expr.h>

#include <library/cpp/svnversion/svnversion.h>

#include <util/stream/file.h>
#include <util/folder/path.h>
#include <util/string/split.h>
#include <util/generic/yexception.h>

#include <sstream>

#include <contrib/libs/dtl/dtl/dtl.hpp>

using namespace NYql;

std::string CalculateDiff(const TString& oldAst, const TString& newAst) {
    auto oldLines = StringSplitter(oldAst).Split('\n').ToList<std::string>();
    auto newLines = StringSplitter(newAst).Split('\n').ToList<std::string>();

    dtl::Diff<std::string, TVector<std::string>> d(oldLines, newLines);
    d.compose();
    d.composeUnifiedHunks();

    std::ostringstream ss;
    d.printUnifiedFormat(ss);
    return ss.str();
}


const int DIFF_LINES_LIMIT = 16;

void DumpSmallNodes(const TExprNode* rootOne, const TExprNode* rootTwo) {
    const auto isDumpSmall = [] (const TString& dump) {
        return std::count(dump.begin(), dump.end(), '\n') < DIFF_LINES_LIMIT;
    };
    const auto rootOneDump = rootOne->Dump();
    if (!isDumpSmall(rootOneDump)) {
        return;
    }
    const auto rootTwoDump = rootTwo->Dump();
    if (!isDumpSmall(rootTwoDump)) {
        return;
    }

    Cerr << rootOneDump << '\n' << rootTwoDump;
}

int Main(int argc, const char *argv[])
{
    if (argc != 3) {
        PrintProgramSvnVersion();
        Cout << Endl << "Usage: " << argv[0] << " <fileone> <filetwo>" << Endl;
        return 2;
    }

    const TString fileOne(argv[1]), fileTwo(argv[2]);
    const TString progOneAst = TFileInput(fileOne).ReadAll();
    const TString progTwoAst = TFileInput(fileTwo).ReadAll();
    const auto progOne(ParseAst(progOneAst)), progTwo(ParseAst(progTwoAst));

    if (!(progOne.IsOk() && progTwo.IsOk())) {
        if (!progOne.IsOk()) {
            Cerr << "Errors in " << fileOne << Endl;
            progOne.Issues.PrintTo(Cerr);
        }
        if (!progTwo.IsOk()) {
            Cerr << "Errors in " << fileTwo << Endl;
            progTwo.Issues.PrintTo(Cerr);
        }
        return 3;
    }

    TExprContext ctxOne, ctxTwo;
    TExprNode::TPtr exprOne, exprTwo;

    const bool okOne = CompileExpr(*progOne.Root, exprOne, ctxOne, nullptr, nullptr);
    const bool okTwo = CompileExpr(*progTwo.Root, exprTwo, ctxTwo, nullptr, nullptr);

    if (!(okOne && okTwo)) {
        if (!okOne) {
            Cerr << "Errors on compile " << fileOne << Endl;
            ctxOne.IssueManager.GetIssues().PrintTo(Cerr);
        }
        if (!okTwo) {
            Cerr << "Errors on compile " << fileTwo << Endl;
            ctxTwo.IssueManager.GetIssues().PrintTo(Cerr);
        }
        return 4;
    }

    const TExprNode* rootOne = exprOne.Get();
    const TExprNode* rootTwo = exprTwo.Get();

    auto rootOnePos = ctxOne.GetPosition(rootOne->Pos());
    auto rootTwoPos = ctxTwo.GetPosition(rootTwo->Pos());
    if (!CompareExprTrees(rootOne, rootTwo)) {
        const auto diff = CalculateDiff(progOneAst, progTwoAst);

        Cerr << "Programs are not equal!" << Endl;
        if (rootOne->Type() != rootTwo->Type()) {
            Cerr << "Node in " << fileOne << " at [" << rootOnePos.Row << ":" << rootOnePos.Column << "] type is " << rootOne->Type() << Endl;
            Cerr << "Node in " << fileTwo << " at [" << rootTwoPos.Row << ":" << rootTwoPos.Column << "] type is " << rootTwo->Type() << Endl;
            Cerr << "\nFile diff:\n" << diff;
        } else if (rootOne->ChildrenSize() != rootTwo->ChildrenSize()) {
            Cerr << "Node '" << rootOne->Content() << "' in " << fileOne << " at [" << rootOnePos.Row << ":" << rootOnePos.Column << "] has " << rootOne->ChildrenSize() << " children." << Endl;
            Cerr << "Node '" << rootTwo->Content() << "' in " << fileTwo << " at [" << rootTwoPos.Row << ":" << rootTwoPos.Column << "] has " << rootTwo->ChildrenSize() << " children." << Endl;
            DumpSmallNodes(rootOne, rootTwo);
            Cerr << "\nFile diff:\n" << diff;
        } else {
            Cerr << "Node in " << fileOne << " at [" << rootOnePos.Row << ":" << rootOnePos.Column << "]:";
            Cerr << "Node in " << fileTwo << " at [" << rootTwoPos.Row << ":" << rootTwoPos.Column << "]:";
            DumpSmallNodes(rootOne, rootTwo);
            Cerr << "\nFile diff:\n" << diff;
        }
        return 5;
    }

    return 0;
}

int main(int argc, const char *argv[]) {
    NYql::NBacktrace::RegisterKikimrFatalActions();
    NYql::NBacktrace::EnableKikimrSymbolize();

    try {
        return Main(argc, argv);
    }
    catch (...) {
        Cerr << CurrentExceptionMessage() << Endl;
        return 1;
    }
}