| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2019 Christian Gagneraud <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "FixItExporter.h" |
| 8 |
|
| 9 |
#include <clang/Basic/SourceManager.h> |
| 10 |
#include <clang/Frontend/FrontendDiagnostic.h> |
| 11 |
#include <clang/Rewrite/Frontend/FixItRewriter.h> |
| 12 |
#include <clang/Tooling/DiagnosticsYaml.h> |
| 13 |
|
| 14 |
// #define DEBUG_FIX_IT_EXPORTER |
| 15 |
|
| 16 |
using namespace clang; |
| 17 |
|
| 18 |
static clang::tooling::TranslationUnitDiagnostics &getTuDiag() |
| 19 |
{ |
| 20 |
static clang::tooling::TranslationUnitDiagnostics s_tudiag; |
| 21 |
return s_tudiag; |
| 22 |
} |
| 23 |
|
| 24 |
FixItExporter::FixItExporter(DiagnosticsEngine &DiagEngine, |
| 25 |
SourceManager &SourceMgr, |
| 26 |
const LangOptions &LangOpts, |
| 27 |
const std::string &exportFixes, |
| 28 |
bool isClazyStandalone) |
| 29 |
: DiagEngine(DiagEngine) |
| 30 |
, SourceMgr(SourceMgr) |
| 31 |
, LangOpts(LangOpts) |
| 32 |
, exportFixes(exportFixes) |
| 33 |
{ |
| 34 |
if (!isClazyStandalone) { |
| 35 |
// When using clazy as plugin each translation unit fixes goes to a separate YAML file |
| 36 |
getTuDiag().Diagnostics.clear(); |
| 37 |
} |
| 38 |
|
| 39 |
Owner = DiagEngine.takeClient(); |
| 40 |
Client = DiagEngine.getClient(); |
| 41 |
DiagEngine.setClient(this, false); |
| 42 |
} |
| 43 |
|
| 44 |
FixItExporter::~FixItExporter() |
| 45 |
{ |
| 46 |
if (Client) { |
| 47 |
DiagEngine.setClient(Client, Owner.release() != nullptr); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
void FixItExporter::BeginSourceFile(const LangOptions &LangOpts, const Preprocessor *PP) |
| 52 |
{ |
| 53 |
if (Client) { |
| 54 |
Client->BeginSourceFile(LangOpts, PP); |
| 55 |
} |
| 56 |
|
| 57 |
const auto id = SourceMgr.getMainFileID(); |
| 58 |
const auto entry = SourceMgr.getFileEntryRefForID(id); |
| 59 |
getTuDiag().MainSourceFile = entry->getName().str(); |
| 60 |
} |
| 61 |
|
| 62 |
bool FixItExporter::IncludeInDiagnosticCounts() const |
| 63 |
{ |
| 64 |
return Client ? Client->IncludeInDiagnosticCounts() : true; |
| 65 |
} |
| 66 |
|
| 67 |
void FixItExporter::EndSourceFile() |
| 68 |
{ |
| 69 |
if (Client) { |
| 70 |
Client->EndSourceFile(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
tooling::Diagnostic FixItExporter::ConvertDiagnostic(const Diagnostic &Info) |
| 75 |
{ |
| 76 |
SmallString<256> TmpMessageText; |
| 77 |
Info.FormatDiagnostic(TmpMessageText); |
| 78 |
// TODO: This returns an empty string: DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(Info.getID()); |
| 79 |
// HACK: capture it at the end of the message: Message text [check-name] |
| 80 |
|
| 81 |
std::string checkName = DiagEngine.getDiagnosticIDs()->getWarningOptionForDiag(Info.getID()).str(); |
| 82 |
std::string messageText; |
| 83 |
|
| 84 |
if (checkName.empty()) { |
| 85 |
// Non-built-in clang warnings have the [checkName] in the message |
| 86 |
messageText = TmpMessageText.slice(0, TmpMessageText.find_last_of('[') - 1).str(); |
| 87 |
|
| 88 |
checkName = TmpMessageText.slice(TmpMessageText.find_last_of('[') + 3, TmpMessageText.find_last_of(']')).str(); |
| 89 |
} else { |
| 90 |
messageText = TmpMessageText.c_str(); |
| 91 |
} |
| 92 |
|
| 93 |
llvm::StringRef CurrentBuildDir; // Not needed? |
| 94 |
|
| 95 |
tooling::Diagnostic ToolingDiag(checkName, tooling::Diagnostic::Warning, CurrentBuildDir); |
| 96 |
// FIXME: Sometimes the file path is an empty string. |
| 97 |
if (Info.getLocation().isMacroID()) { |
| 98 |
auto MacroLoc = SourceMgr.getFileLoc(Info.getLocation()); |
| 99 |
ToolingDiag.Message = tooling::DiagnosticMessage(messageText, SourceMgr, MacroLoc); |
| 100 |
} else { |
| 101 |
ToolingDiag.Message = tooling::DiagnosticMessage(messageText, SourceMgr, Info.getLocation()); |
| 102 |
} |
| 103 |
return ToolingDiag; |
| 104 |
} |
| 105 |
|
| 106 |
tooling::Replacement FixItExporter::ConvertFixIt(const FixItHint &Hint) |
| 107 |
{ |
| 108 |
// TODO: Proper handling of macros |
| 109 |
// https://stackoverflow.com/questions/24062989/clang-fails-replacing-a-statement-if-it-contains-a-macro |
| 110 |
tooling::Replacement Replacement; |
| 111 |
if (Hint.CodeToInsert.empty()) { |
| 112 |
if (Hint.InsertFromRange.isValid()) { |
| 113 |
clang::SourceLocation b(Hint.InsertFromRange.getBegin()); |
| 114 |
clang::SourceLocation _e(Hint.InsertFromRange.getEnd()); |
| 115 |
if (b.isMacroID()) { |
| 116 |
b = SourceMgr.getSpellingLoc(b); |
| 117 |
} |
| 118 |
if (_e.isMacroID()) { |
| 119 |
_e = SourceMgr.getSpellingLoc(_e); |
| 120 |
} |
| 121 |
clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, SourceMgr, LangOpts)); |
| 122 |
StringRef Text(SourceMgr.getCharacterData(b), SourceMgr.getCharacterData(e) - SourceMgr.getCharacterData(b)); |
| 123 |
return {SourceMgr, Hint.RemoveRange, Text}; |
| 124 |
} |
| 125 |
return {SourceMgr, Hint.RemoveRange, ""}; |
| 126 |
} |
| 127 |
return {SourceMgr, Hint.RemoveRange, Hint.CodeToInsert}; |
| 128 |
} |
| 129 |
|
| 130 |
// This function should export diagnostics, even if they are not fixits! Needed for Qtcreator integration |
| 131 |
void FixItExporter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) |
| 132 |
{ |
| 133 |
// Default implementation (Warnings/errors count). |
| 134 |
DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); |
| 135 |
|
| 136 |
// Let original client do it's handling |
| 137 |
if (Client) { |
| 138 |
Client->HandleDiagnostic(DiagLevel, Info); |
| 139 |
} |
| 140 |
|
| 141 |
// Convert and record warning diagnostics and their notes |
| 142 |
if (DiagLevel == DiagnosticsEngine::Warning) { |
| 143 |
auto ToolingDiag = ConvertDiagnostic(Info); |
| 144 |
for (unsigned Idx = 0, Last = Info.getNumFixItHints(); Idx < Last; ++Idx) { |
| 145 |
const FixItHint &Hint = Info.getFixItHint(Idx); |
| 146 |
const auto replacement = ConvertFixIt(Hint); |
| 147 |
#ifdef DEBUG_FIX_IT_EXPORTER |
| 148 |
const auto FileName = SourceMgr.getFilename(Info.getLocation()); |
| 149 |
llvm::errs() << "Handling Fixit #" << Idx << " for " << FileName.str() << "\n"; |
| 150 |
llvm::errs() << "F: " << Hint.RemoveRange.getBegin().printToString(SourceMgr) << ":" << Hint.RemoveRange.getEnd().printToString(SourceMgr) << " " |
| 151 |
<< Hint.InsertFromRange.getBegin().printToString(SourceMgr) << ":" << Hint.InsertFromRange.getEnd().printToString(SourceMgr) << " " |
| 152 |
<< Hint.BeforePreviousInsertions << " " << Hint.CodeToInsert << "\n"; |
| 153 |
llvm::errs() << "R: " << replacement.toString() << "\n"; |
| 154 |
#endif |
| 155 |
clang::tooling::Replacements &Replacements = ToolingDiag.Message.Fix[replacement.getFilePath()]; |
| 156 |
llvm::Error error = Replacements.add(ConvertFixIt(Hint)); |
| 157 |
if (error) { |
| 158 |
Diag(Info.getLocation(), diag::note_fixit_failed); |
| 159 |
} |
| 160 |
} |
| 161 |
getTuDiag().Diagnostics.push_back(ToolingDiag); |
| 162 |
m_recordNotes = true; |
| 163 |
} |
| 164 |
// FIXME: We do not receive notes. |
| 165 |
else if (DiagLevel == DiagnosticsEngine::Note && m_recordNotes) { |
| 166 |
#ifdef DEBUG_FIX_IT_EXPORTER |
| 167 |
const auto FileName = SourceMgr.getFilename(Info.getLocation()); |
| 168 |
llvm::errs() << "Handling Note for " << FileName.str() << "\n"; |
| 169 |
#endif |
| 170 |
auto diags = getTuDiag().Diagnostics.back(); |
| 171 |
auto diag = ConvertDiagnostic(Info); |
| 172 |
diags.Notes.append(1, diag.Message); |
| 173 |
} else { |
| 174 |
m_recordNotes = false; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
void FixItExporter::Export() |
| 179 |
{ |
| 180 |
auto tuDiag = getTuDiag(); |
| 181 |
if (!tuDiag.Diagnostics.empty()) { |
| 182 |
std::error_code EC; |
| 183 |
llvm::raw_fd_ostream OS(exportFixes, EC, llvm::sys::fs::OF_None); |
| 184 |
llvm::yaml::Output YAML(OS); |
| 185 |
YAML << getTuDiag(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void FixItExporter::Diag(SourceLocation Loc, unsigned DiagID) |
| 190 |
{ |
| 191 |
// When producing this diagnostic, we temporarily bypass ourselves, |
| 192 |
// clear out any current diagnostic, and let the downstream client |
| 193 |
// format the diagnostic. |
| 194 |
DiagEngine.setClient(Client, false); |
| 195 |
#if LLVM_VERSION_MAJOR < 20 |
| 196 |
DiagEngine.Clear(); |
| 197 |
#endif |
| 198 |
DiagEngine.Report(Loc, DiagID); |
| 199 |
DiagEngine.setClient(this, false); |
| 200 |
} |