aboutsummaryrefslogtreecommitdiffstats
path: root/src/FixItExporter.cpp
blob: 508a2b75cd2591fc768d32425ea8ff4d9533ce69 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
  This file is part of the clazy static checker.

    Copyright (C) 2019 Christian Gagneraud <chgans@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "FixItExporter.h"
#include "SourceCompatibilityHelpers.h"

#include <clang/Frontend/FrontendDiagnostic.h>
#include <clang/Tooling/DiagnosticsYaml.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Rewrite/Frontend/FixItRewriter.h>

// #define DEBUG_FIX_IT_EXPORTER

using namespace clang;

static clang::tooling::TranslationUnitDiagnostics& getTuDiag()
{
    static clang::tooling::TranslationUnitDiagnostics s_tudiag;
    return s_tudiag;
}

FixItExporter::FixItExporter(DiagnosticsEngine &DiagEngine, SourceManager &SourceMgr,
                             const LangOptions &LangOpts, const std::string &exportFixes,
                             bool isClazyStandalone)
    : DiagEngine(DiagEngine)
    , SourceMgr(SourceMgr)
    , LangOpts(LangOpts)
    , exportFixes(exportFixes)
{
    if (!isClazyStandalone) {
        // When using clazy as plugin each translation unit fixes goes to a separate YAML file
        getTuDiag().Diagnostics.clear();
    }

    Owner = DiagEngine.takeClient();
    Client = DiagEngine.getClient();
    DiagEngine.setClient(this, false);
}

FixItExporter::~FixItExporter()
{
    if (Client)
        DiagEngine.setClient(Client, Owner.release() != nullptr);
}

void FixItExporter::BeginSourceFile(const LangOptions &LangOpts, const Preprocessor *PP)
{
    if (Client)
        Client->BeginSourceFile(LangOpts, PP);

    const auto id = SourceMgr.getMainFileID();
    const auto entry = SourceMgr.getFileEntryForID(id);
    getTuDiag().MainSourceFile = static_cast<std::string>(entry->getName());
}

bool FixItExporter::IncludeInDiagnosticCounts() const
{
    return Client ? Client->IncludeInDiagnosticCounts() : true;
}

void FixItExporter::EndSourceFile()
{
    if (Client)
        Client->EndSourceFile();
}

tooling::Diagnostic FixItExporter::ConvertDiagnostic(const Diagnostic &Info)
{
    SmallString<256> TmpMessageText;
    Info.FormatDiagnostic(TmpMessageText);
    // TODO: This returns an empty string: DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(Info.getID());
    // HACK: capture it at the end of the message: Message text [check-name]

    std::string checkName =
        static_cast<std::string>(DiagEngine.getDiagnosticIDs()->getWarningOptionForDiag(Info.getID()));
    std::string messageText;

    if (checkName.empty()) {
        // Non-built-in clang warnings have the [checkName] in the message
        messageText = TmpMessageText.slice(0, TmpMessageText.find_last_of('[') - 1).str();

        checkName = TmpMessageText.slice(TmpMessageText.find_last_of('[') + 3,
                                         TmpMessageText.find_last_of(']')).str();
    } else {
         messageText = TmpMessageText.c_str();
    }


    llvm::StringRef CurrentBuildDir; // Not needed?

    tooling::Diagnostic ToolingDiag(checkName,
                                    tooling::Diagnostic::Warning,
                                    CurrentBuildDir);
    // FIXME: Sometimes the file path is an empty string.
    if (Info.getLocation().isMacroID()) {
        auto MacroLoc = SourceMgr.getFileLoc(Info.getLocation());
        ToolingDiag.Message = tooling::DiagnosticMessage(messageText, SourceMgr, MacroLoc);
    } else {
        ToolingDiag.Message = tooling::DiagnosticMessage(messageText, SourceMgr, Info.getLocation());
    }
    return ToolingDiag;
}

tooling::Replacement FixItExporter::ConvertFixIt(const FixItHint &Hint)
{
    // TODO: Proper handling of macros
    // https://stackoverflow.com/questions/24062989/clang-fails-replacing-a-statement-if-it-contains-a-macro
    tooling::Replacement Replacement;
    if (Hint.CodeToInsert.empty()) {
        if (Hint.InsertFromRange.isValid()) {
            clang::SourceLocation b(Hint.InsertFromRange.getBegin()), _e(Hint.InsertFromRange.getEnd());
            if (b.isMacroID())
                b = SourceMgr.getSpellingLoc(b);
            if (_e.isMacroID())
                _e = SourceMgr.getSpellingLoc(_e);
            clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, SourceMgr, LangOpts));
            StringRef Text(SourceMgr.getCharacterData(b),
                           SourceMgr.getCharacterData(e) - SourceMgr.getCharacterData(b));
            return tooling::Replacement(SourceMgr, Hint.RemoveRange, Text);
        }
        return tooling::Replacement(SourceMgr, Hint.RemoveRange, "");
    }
    return tooling::Replacement(SourceMgr, Hint.RemoveRange, Hint.CodeToInsert);
}

void FixItExporter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info)
{
    // Default implementation (Warnings/errors count).
    DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);

    // Let original client do it's handling
    if (Client)
        Client->HandleDiagnostic(DiagLevel, Info);

    // Convert and record warning diagnostics and their notes
    if (DiagLevel == DiagnosticsEngine::Warning) {
        auto ToolingDiag = ConvertDiagnostic(Info);
        for (unsigned Idx = 0, Last = Info.getNumFixItHints();
             Idx < Last; ++Idx) {
            const FixItHint &Hint = Info.getFixItHint(Idx);
            const auto replacement = ConvertFixIt(Hint);
#ifdef DEBUG_FIX_IT_EXPORTER
            const auto FileName = SourceMgr.getFilename(Info.getLocation());
            llvm::errs() << "Handling Fixit #" << Idx << " for " << FileName.str() << "\n";
            llvm::errs() << "F: "
                      << Hint.RemoveRange.getBegin().printToString(SourceMgr) << ":"
                      << Hint.RemoveRange.getEnd().printToString(SourceMgr) << " "
                      << Hint.InsertFromRange.getBegin().printToString(SourceMgr) << ":"
                      << Hint.InsertFromRange.getEnd().printToString(SourceMgr) << " "
                      << Hint.BeforePreviousInsertions << " "
                      << Hint.CodeToInsert << "\n";
            llvm::errs() << "R: " << replacement.toString() << "\n";
#endif
            clang::tooling::Replacements &Replacements = clazy::DiagnosticFix(ToolingDiag, replacement.getFilePath());
            llvm::Error error = Replacements.add(ConvertFixIt(Hint));
            if (error) {
                Diag(Info.getLocation(), diag::note_fixit_failed);
            }
        }
        getTuDiag().Diagnostics.push_back(ToolingDiag);
        m_recordNotes = true;
    }
    // FIXME: We do not receive notes.
    else if (DiagLevel == DiagnosticsEngine::Note && m_recordNotes) {
#ifdef DEBUG_FIX_IT_EXPORTER
        const auto FileName = SourceMgr.getFilename(Info.getLocation());
        llvm::errs() << "Handling Note for " << FileName.str() << "\n";
#endif
        auto diags = getTuDiag().Diagnostics.back();
        auto diag = ConvertDiagnostic(Info);
        diags.Notes.append(1, diag.Message);
    }
    else {
        m_recordNotes = false;
    }
}

void FixItExporter::Export()
{
    auto tuDiag = getTuDiag();
    if (!tuDiag.Diagnostics.empty()) {
        std::error_code EC;
        llvm::raw_fd_ostream OS(exportFixes, EC, llvm::sys::fs::OF_None);
        llvm::yaml::Output YAML(OS);
        YAML << getTuDiag();
    }
}

void FixItExporter::Diag(SourceLocation Loc, unsigned DiagID)
{
    // When producing this diagnostic, we temporarily bypass ourselves,
    // clear out any current diagnostic, and let the downstream client
    // format the diagnostic.
    DiagEngine.setClient(Client, false);
    DiagEngine.Clear();
    DiagEngine.Report(Loc, DiagID);
    DiagEngine.setClient(this, false);
}