[clang-tools-extra] r358576 - [clang-tidy] Add fix descriptions to clang-tidy checks.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 17 05:54:00 PDT 2019
Author: hokein
Date: Wed Apr 17 05:53:59 2019
New Revision: 358576
URL: http://llvm.org/viewvc/llvm-project?rev=358576&view=rev
Log:
[clang-tidy] Add fix descriptions to clang-tidy checks.
Summary:
Motivation/Context: in the code review system integrating with clang-tidy,
clang-tidy doesn't provide a human-readable description of the fix. Usually
developers have to preview a code diff (before vs after apply the fix) to
understand what the fix does before applying a fix.
This patch proposes that each clang-tidy check provides a short and
actional fix description that can be shown in the UI, so that users can know
what the fix does without previewing diff.
This patch extends clang-tidy framework to support fix descriptions (will add implementations for
existing checks in the future). Fix descriptions and fixes are emitted via diagnostic::Note (rather than
attaching the main warning diagnostic).
Before this patch:
```
void MyCheck::check(...) {
...
diag(loc, "my check warning") << FixtItHint::CreateReplacement(...);
}
```
After:
```
void MyCheck::check(...) {
...
diag(loc, "my check warning"); // Emit a check warning
diag(loc, "fix description", DiagnosticIDs::Note) << FixtItHint::CreateReplacement(...); // Emit a diagnostic note and a fix
}
```
Reviewers: sammccall, alexfh
Reviewed By: alexfh
Subscribers: MyDeveloperDay, Eugene.Zelenko, aaron.ballman, JonasToth, xazax.hun, jdoerfert, cfe-commits
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D59932
Modified:
clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
clang-tools-extra/trunk/clang-tidy/add_new_check.py
clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file1.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file2.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file1.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file2.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file3.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/crlf/file1.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/no.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/yes.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file1.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file2.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file1.yaml
clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file2.yaml
clang-tools-extra/trunk/test/clang-tidy/export-diagnostics.cpp
clang-tools-extra/trunk/unittests/clang-apply-replacements/ApplyReplacementsTest.cpp
clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
Modified: clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp (original)
+++ clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp Wed Apr 17 05:53:59 2019
@@ -19,6 +19,7 @@
#include "clang/Format/Format.h"
#include "clang/Lex/Lexer.h"
#include "clang/Rewrite/Core/Rewriter.h"
+#include "clang/Tooling/Core/Diagnostic.h"
#include "clang/Tooling/DiagnosticsYaml.h"
#include "clang/Tooling/ReplacementsYaml.h"
#include "llvm/ADT/ArrayRef.h"
@@ -169,9 +170,11 @@ groupReplacements(const TUReplacements &
for (const auto &TU : TUDs)
for (const auto &D : TU.Diagnostics)
- for (const auto &Fix : D.Fix)
- for (const tooling::Replacement &R : Fix.second)
- AddToGroup(R, true);
+ if (const auto *ChoosenFix = tooling::selectFirstFix(D)) {
+ for (const auto &Fix : *ChoosenFix)
+ for (const tooling::Replacement &R : Fix.second)
+ AddToGroup(R, true);
+ }
// Sort replacements per file to keep consistent behavior when
// clang-apply-replacements run on differents machine.
Modified: clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp Wed Apr 17 05:53:59 2019
@@ -35,6 +35,7 @@
#include "clang/Lex/Preprocessor.h"
#include "clang/Rewrite/Frontend/FixItRewriter.h"
#include "clang/Rewrite/Frontend/FrontendActions.h"
+#include "clang/Tooling/Core/Diagnostic.h"
#if CLANG_ENABLE_STATIC_ANALYZER
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
#include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
@@ -125,15 +126,17 @@ public:
}
auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
<< Message.Message << Name;
- for (const auto &FileAndReplacements : Error.Fix) {
- for (const auto &Repl : FileAndReplacements.second) {
- SourceLocation FixLoc;
- ++TotalFixes;
- bool CanBeApplied = false;
- if (Repl.isApplicable()) {
- SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
- Files.makeAbsolutePath(FixAbsoluteFilePath);
- if (ApplyFixes) {
+ // FIXME: explore options to support interactive fix selection.
+ const llvm::StringMap<Replacements> *ChosenFix = selectFirstFix(Error);
+ if (ApplyFixes && ChosenFix) {
+ for (const auto &FileAndReplacements : *ChosenFix) {
+ for (const auto &Repl : FileAndReplacements.second) {
+ ++TotalFixes;
+ bool CanBeApplied = false;
+ if (Repl.isApplicable()) {
+ SourceLocation FixLoc;
+ SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+ Files.makeAbsolutePath(FixAbsoluteFilePath);
tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
Repl.getLength(),
Repl.getReplacementText());
@@ -158,28 +161,17 @@ public:
llvm::errs()
<< "Can't resolve conflict, skipping the replacement.\n";
}
-
} else {
CanBeApplied = true;
++AppliedFixes;
}
+ FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
+ FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
}
- FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
- SourceLocation FixEndLoc =
- FixLoc.getLocWithOffset(Repl.getLength());
- // Retrieve the source range for applicable fixes. Macro definitions
- // on the command line have locations in a virtual buffer and don't
- // have valid file paths and are therefore not applicable.
- CharSourceRange Range =
- CharSourceRange::getCharRange(SourceRange(FixLoc, FixEndLoc));
- Diag << FixItHint::CreateReplacement(Range,
- Repl.getReplacementText());
}
-
- if (ApplyFixes)
- FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
}
}
+ reportFix(Diag, Error.Message.Fix);
}
for (auto Fix : FixLocations) {
Diags.Report(Fix.first, Fix.second ? diag::note_fixit_applied
@@ -250,10 +242,33 @@ private:
return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
}
+ void reportFix(const DiagnosticBuilder &Diag,
+ const llvm::StringMap<Replacements> &Fix) {
+ for (const auto &FileAndReplacements : Fix) {
+ for (const auto &Repl : FileAndReplacements.second) {
+ if (!Repl.isApplicable())
+ continue;
+ SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+ Files.makeAbsolutePath(FixAbsoluteFilePath);
+ SourceLocation FixLoc =
+ getLocation(FixAbsoluteFilePath, Repl.getOffset());
+ SourceLocation FixEndLoc = FixLoc.getLocWithOffset(Repl.getLength());
+ // Retrieve the source range for applicable fixes. Macro definitions
+ // on the command line have locations in a virtual buffer and don't
+ // have valid file paths and are therefore not applicable.
+ CharSourceRange Range =
+ CharSourceRange::getCharRange(SourceRange(FixLoc, FixEndLoc));
+ Diag << FixItHint::CreateReplacement(Range, Repl.getReplacementText());
+ }
+ }
+ }
+
void reportNote(const tooling::DiagnosticMessage &Message) {
SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
- Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
+ auto Diag =
+ Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
<< Message.Message;
+ reportFix(Diag, Message.Fix);
}
FileManager Files;
Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp Wed Apr 17 05:53:59 2019
@@ -18,8 +18,10 @@
#include "ClangTidyDiagnosticConsumer.h"
#include "ClangTidyOptions.h"
#include "clang/AST/ASTDiagnostic.h"
+#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/DiagnosticRenderer.h"
+#include "clang/Tooling/Core/Diagnostic.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include <tuple>
@@ -68,6 +70,9 @@ protected:
SmallVectorImpl<CharSourceRange> &Ranges,
ArrayRef<FixItHint> Hints) override {
assert(Loc.isValid());
+ tooling::DiagnosticMessage *DiagWithFix =
+ Level == DiagnosticsEngine::Note ? &Error.Notes.back() : &Error.Message;
+
for (const auto &FixIt : Hints) {
CharSourceRange Range = FixIt.RemoveRange;
assert(Range.getBegin().isValid() && Range.getEnd().isValid() &&
@@ -77,7 +82,8 @@ protected:
tooling::Replacement Replacement(Loc.getManager(), Range,
FixIt.CodeToInsert);
- llvm::Error Err = Error.Fix[Replacement.getFilePath()].add(Replacement);
+ llvm::Error Err =
+ DiagWithFix->Fix[Replacement.getFilePath()].add(Replacement);
// FIXME: better error handling (at least, don't let other replacements be
// applied).
if (Err) {
@@ -581,9 +587,17 @@ void ClangTidyDiagnosticConsumer::remove
// Compute error sizes.
std::vector<int> Sizes;
- for (const auto &Error : Errors) {
+ std::vector<
+ std::pair<ClangTidyError *, llvm::StringMap<tooling::Replacements> *>>
+ ErrorFixes;
+ for (auto &Error : Errors) {
+ if (const auto *Fix = tooling::selectFirstFix(Error))
+ ErrorFixes.emplace_back(
+ &Error, const_cast<llvm::StringMap<tooling::Replacements> *>(Fix));
+ }
+ for (const auto &ErrorAndFix : ErrorFixes) {
int Size = 0;
- for (const auto &FileAndReplaces : Error.Fix) {
+ for (const auto &FileAndReplaces : *ErrorAndFix.second) {
for (const auto &Replace : FileAndReplaces.second)
Size += Replace.getLength();
}
@@ -592,8 +606,8 @@ void ClangTidyDiagnosticConsumer::remove
// Build events from error intervals.
std::map<std::string, std::vector<Event>> FileEvents;
- for (unsigned I = 0; I < Errors.size(); ++I) {
- for (const auto &FileAndReplace : Errors[I].Fix) {
+ for (unsigned I = 0; I < ErrorFixes.size(); ++I) {
+ for (const auto &FileAndReplace : *ErrorFixes[I].second) {
for (const auto &Replace : FileAndReplace.second) {
unsigned Begin = Replace.getOffset();
unsigned End = Begin + Replace.getLength();
@@ -608,7 +622,7 @@ void ClangTidyDiagnosticConsumer::remove
}
}
- std::vector<bool> Apply(Errors.size(), true);
+ std::vector<bool> Apply(ErrorFixes.size(), true);
for (auto &FileAndEvents : FileEvents) {
std::vector<Event> &Events = FileAndEvents.second;
// Sweep.
@@ -627,10 +641,10 @@ void ClangTidyDiagnosticConsumer::remove
assert(OpenIntervals == 0 && "Amount of begin/end points doesn't match");
}
- for (unsigned I = 0; I < Errors.size(); ++I) {
+ for (unsigned I = 0; I < ErrorFixes.size(); ++I) {
if (!Apply[I]) {
- Errors[I].Fix.clear();
- Errors[I].Notes.emplace_back(
+ ErrorFixes[I].second->clear();
+ ErrorFixes[I].first->Notes.emplace_back(
"this fix will not be applied because it overlaps with another fix");
}
}
Modified: clang-tools-extra/trunk/clang-tidy/add_new_check.py
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/add_new_check.py?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/add_new_check.py (original)
+++ clang-tools-extra/trunk/clang-tidy/add_new_check.py Wed Apr 17 05:53:59 2019
@@ -137,7 +137,8 @@ void %(check_name)s::check(const MatchFi
if (MatchedDecl->getName().startswith("awesome_"))
return;
diag(MatchedDecl->getLocation(), "function %%0 is insufficiently awesome")
- << MatchedDecl
+ << MatchedDecl;
+ diag(MatchedDecl->getLocation(), "insert 'awesome'", DiagnosticIDs::Note)
<< FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
}
Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp Wed Apr 17 05:53:59 2019
@@ -154,7 +154,10 @@ void UnusedUsingDeclsCheck::onEndOfTrans
for (const auto &Context : Contexts) {
if (!Context.IsUsed) {
diag(Context.FoundUsingDecl->getLocation(), "using decl %0 is unused")
- << Context.FoundUsingDecl
+ << Context.FoundUsingDecl;
+ // Emit a fix and a fix description of the check;
+ diag(Context.FoundUsingDecl->getLocation(),
+ /*FixDescription=*/"remove the using", DiagnosticIDs::Note)
<< FixItHint::CreateRemoval(Context.UsingDeclRange);
}
}
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file1.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file1.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file1.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file1.yaml Wed Apr 17 05:53:59 2019
@@ -2,24 +2,25 @@
MainSourceFile: source1.cpp
Diagnostics:
- DiagnosticName: test-basic
- Message: Fix
- FilePath: $(path)/basic.h
- FileOffset: 242
- Replacements:
- - FilePath: $(path)/basic.h
- Offset: 242
- Length: 26
- ReplacementText: 'auto & elem : ints'
- - FilePath: $(path)/basic.h
- Offset: 276
- Length: 22
- ReplacementText: ''
- - FilePath: $(path)/basic.h
- Offset: 298
- Length: 1
- ReplacementText: elem
- - FilePath: $(path)/../basic/basic.h
- Offset: 148
- Length: 0
- ReplacementText: 'override '
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/basic.h
+ FileOffset: 242
+ Replacements:
+ - FilePath: $(path)/basic.h
+ Offset: 242
+ Length: 26
+ ReplacementText: 'auto & elem : ints'
+ - FilePath: $(path)/basic.h
+ Offset: 276
+ Length: 22
+ ReplacementText: ''
+ - FilePath: $(path)/basic.h
+ Offset: 298
+ Length: 1
+ ReplacementText: elem
+ - FilePath: $(path)/../basic/basic.h
+ Offset: 148
+ Length: 0
+ ReplacementText: 'override '
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file2.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file2.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file2.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/basic/file2.yaml Wed Apr 17 05:53:59 2019
@@ -2,12 +2,13 @@
MainSourceFile: source2.cpp
Diagnostics:
- DiagnosticName: test-basic
- Message: Fix
- FilePath: $(path)/basic.h
- FileOffset: 148
- Replacements:
- - FilePath: $(path)/../basic/basic.h
- Offset: 298
- Length: 1
- ReplacementText: elem
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/basic.h
+ FileOffset: 148
+ Replacements:
+ - FilePath: $(path)/../basic/basic.h
+ Offset: 298
+ Length: 1
+ ReplacementText: elem
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file1.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file1.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file1.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file1.yaml Wed Apr 17 05:53:59 2019
@@ -2,20 +2,21 @@
MainSourceFile: source1.cpp
Diagnostics:
- DiagnosticName: test-conflict
- Message: Fix
- FilePath: $(path)/common.h
- FileOffset: 106
- Replacements:
- - FilePath: $(path)/common.h
- Offset: 106
- Length: 26
- ReplacementText: 'auto & i : ints'
- - FilePath: $(path)/common.h
- Offset: 140
- Length: 7
- ReplacementText: i
- - FilePath: $(path)/common.h
- Offset: 160
- Length: 12
- ReplacementText: ''
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/common.h
+ FileOffset: 106
+ Replacements:
+ - FilePath: $(path)/common.h
+ Offset: 106
+ Length: 26
+ ReplacementText: 'auto & i : ints'
+ - FilePath: $(path)/common.h
+ Offset: 140
+ Length: 7
+ ReplacementText: i
+ - FilePath: $(path)/common.h
+ Offset: 160
+ Length: 12
+ ReplacementText: ''
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file2.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file2.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file2.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file2.yaml Wed Apr 17 05:53:59 2019
@@ -2,20 +2,21 @@
MainSourceFile: source2.cpp
Diagnostics:
- DiagnosticName: test-conflict
- Message: Fix
- FilePath: $(path)/common.h
- FileOffset: 106
- Replacements:
- - FilePath: $(path)/common.h
- Offset: 106
- Length: 26
- ReplacementText: 'int & elem : ints'
- - FilePath: $(path)/common.h
- Offset: 140
- Length: 7
- ReplacementText: elem
- - FilePath: $(path)/common.h
- Offset: 169
- Length: 1
- ReplacementText: nullptr
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/common.h
+ FileOffset: 106
+ Replacements:
+ - FilePath: $(path)/common.h
+ Offset: 106
+ Length: 26
+ ReplacementText: 'int & elem : ints'
+ - FilePath: $(path)/common.h
+ Offset: 140
+ Length: 7
+ ReplacementText: elem
+ - FilePath: $(path)/common.h
+ Offset: 169
+ Length: 1
+ ReplacementText: nullptr
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file3.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file3.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file3.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/conflict/file3.yaml Wed Apr 17 05:53:59 2019
@@ -2,12 +2,13 @@
MainSourceFile: source1.cpp
Diagnostics:
- DiagnosticName: test-conflict
- Message: Fix
- FilePath: $(path)/common.h
- FileOffset: 169
- Replacements:
- - FilePath: $(path)/common.h
- Offset: 169
- Length: 0
- ReplacementText: "(int*)"
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/common.h
+ FileOffset: 169
+ Replacements:
+ - FilePath: $(path)/common.h
+ Offset: 169
+ Length: 0
+ ReplacementText: "(int*)"
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/crlf/file1.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/crlf/file1.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/crlf/file1.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/crlf/file1.yaml Wed Apr 17 05:53:59 2019
@@ -2,12 +2,13 @@
MainSourceFile: source1.cpp
Diagnostics:
- DiagnosticName: test-crlf
- Message: Fix
- FilePath: $(path)/crlf.cpp
- FileOffset: 79
- Replacements:
- - FilePath: $(path)/crlf.cpp
- Offset: 79
- Length: 1
- ReplacementText: nullptr
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/crlf.cpp
+ FileOffset: 79
+ Replacements:
+ - FilePath: $(path)/crlf.cpp
+ Offset: 79
+ Length: 1
+ ReplacementText: nullptr
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/no.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/no.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/no.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/no.yaml Wed Apr 17 05:53:59 2019
@@ -2,12 +2,13 @@
MainSourceFile: no.cpp
Diagnostics:
- DiagnosticName: test-no
- Message: Fix
- FilePath: $(path)/no.cpp
- FileOffset: 94
- Replacements:
- - FilePath: $(path)/no.cpp
- Offset: 94
- Length: 3
- ReplacementText: 'auto '
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/no.cpp
+ FileOffset: 94
+ Replacements:
+ - FilePath: $(path)/no.cpp
+ Offset: 94
+ Length: 3
+ ReplacementText: 'auto '
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/yes.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/yes.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/yes.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/format/yes.yaml Wed Apr 17 05:53:59 2019
@@ -4,24 +4,25 @@
MainSourceFile: yes.cpp
Diagnostics:
- DiagnosticName: test-yes
- Message: Fix
- FilePath: $(path)/yes.cpp
- FileOffset: 494
- Replacements:
- - FilePath: $(path)/yes.cpp
- Offset: 494
- Length: 1
- ReplacementText: nullptr
- - FilePath: $(path)/yes.cpp
- Offset: 410
- Length: 1
- ReplacementText: nullptr
- - FilePath: $(path)/yes.cpp
- Offset: 454
- Length: 1
- ReplacementText: nullptr
- - FilePath: $(path)/yes.cpp
- Offset: 108
- Length: 38
- ReplacementText: 'auto '
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/yes.cpp
+ FileOffset: 494
+ Replacements:
+ - FilePath: $(path)/yes.cpp
+ Offset: 494
+ Length: 1
+ ReplacementText: nullptr
+ - FilePath: $(path)/yes.cpp
+ Offset: 410
+ Length: 1
+ ReplacementText: nullptr
+ - FilePath: $(path)/yes.cpp
+ Offset: 454
+ Length: 1
+ ReplacementText: nullptr
+ - FilePath: $(path)/yes.cpp
+ Offset: 108
+ Length: 38
+ ReplacementText: 'auto '
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file1.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file1.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file1.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file1.yaml Wed Apr 17 05:53:59 2019
@@ -2,13 +2,14 @@
MainSourceFile: identical.cpp
Diagnostics:
- DiagnosticName: test-identical-insertion
- Message: Fix
- FilePath: $(path)/identical.cpp
- FileOffset: 12
- Replacements:
- - FilePath: $(path)/identical.cpp
- Offset: 12
- Length: 0
- ReplacementText: '0'
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/identical.cpp
+ FileOffset: 12
+ Replacements:
+ - FilePath: $(path)/identical.cpp
+ Offset: 12
+ Length: 0
+ ReplacementText: '0'
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file2.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file2.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file2.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/identical/file2.yaml Wed Apr 17 05:53:59 2019
@@ -2,13 +2,14 @@
MainSourceFile: identical.cpp
Diagnostics:
- DiagnosticName: test-identical-insertion
- Message: Fix
- FilePath: $(path)/identical.cpp
- FileOffset: 12
- Replacements:
- - FilePath: $(path)/identical.cpp
- Offset: 12
- Length: 0
- ReplacementText: '0'
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/identical.cpp
+ FileOffset: 12
+ Replacements:
+ - FilePath: $(path)/identical.cpp
+ Offset: 12
+ Length: 0
+ ReplacementText: '0'
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file1.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file1.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file1.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file1.yaml Wed Apr 17 05:53:59 2019
@@ -2,12 +2,13 @@
MainSourceFile: order-dependent.cpp
Diagnostics:
- DiagnosticName: test-order-dependent-insertion
- Message: Fix
- FilePath: $(path)/order-dependent.cpp
- FileOffset: 12
- Replacements:
- - FilePath: $(path)/order-dependent.cpp
- Offset: 12
- Length: 0
- ReplacementText: '0'
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/order-dependent.cpp
+ FileOffset: 12
+ Replacements:
+ - FilePath: $(path)/order-dependent.cpp
+ Offset: 12
+ Length: 0
+ ReplacementText: '0'
...
Modified: clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file2.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file2.yaml?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file2.yaml (original)
+++ clang-tools-extra/trunk/test/clang-apply-replacements/Inputs/order-dependent/file2.yaml Wed Apr 17 05:53:59 2019
@@ -2,12 +2,13 @@
MainSourceFile: order-dependent.cpp
Diagnostics:
- DiagnosticName: test-order-dependent-insertion
- Message: Fix
- FilePath: $(path)/order-dependent.cpp
- FileOffset: 12
- Replacements:
- - FilePath: $(path)/order-dependent.cpp
- Offset: 12
- Length: 0
- ReplacementText: '1'
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/order-dependent.cpp
+ FileOffset: 12
+ Replacements:
+ - FilePath: $(path)/order-dependent.cpp
+ Offset: 12
+ Length: 0
+ ReplacementText: '1'
...
Modified: clang-tools-extra/trunk/test/clang-tidy/export-diagnostics.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/export-diagnostics.cpp?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/export-diagnostics.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/export-diagnostics.cpp Wed Apr 17 05:53:59 2019
@@ -13,16 +13,19 @@ X(f)
// CHECK-YAML-NEXT: MainSourceFile: '{{.*}}-input.cpp'
// CHECK-YAML-NEXT: Diagnostics:
// CHECK-YAML-NEXT: - DiagnosticName: clang-diagnostic-missing-prototypes
-// CHECK-YAML-NEXT: Message: 'no previous prototype for function ''ff'''
-// CHECK-YAML-NEXT: FileOffset: 30
-// CHECK-YAML-NEXT: FilePath: '{{.*}}-input.cpp'
+// CHECK-YAML-NEXT: DiagnosticMessage:
+// CHECK-YAML-NEXT: Message: 'no previous prototype for function
+// ''ff'''
+// CHECK-YAML-NEXT: FilePath: '{{.*}}-input.cpp'
+// CHECK-YAML-NEXT: FileOffset: 30
+// CHECK-YAML-NEXT: Replacements: []
// CHECK-YAML-NEXT: Notes:
// CHECK-YAML-NEXT: - Message: 'expanded from macro ''X'''
// CHECK-YAML-NEXT: FilePath: '{{.*}}-input.cpp'
// CHECK-YAML-NEXT: FileOffset: 18
+// CHECK-YAML-NEXT: Replacements: []
// CHECK-YAML-NEXT: - Message: expanded from here
// CHECK-YAML-NEXT: FilePath: ''
// CHECK-YAML-NEXT: FileOffset: 0
-// CHECK-YAML-NEXT: Replacements: []
+// CHECK-YAML-NEXT: Replacements: []
// CHECK-YAML-NEXT: ...
-
Modified: clang-tools-extra/trunk/unittests/clang-apply-replacements/ApplyReplacementsTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-apply-replacements/ApplyReplacementsTest.cpp?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-apply-replacements/ApplyReplacementsTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-apply-replacements/ApplyReplacementsTest.cpp Wed Apr 17 05:53:59 2019
@@ -26,7 +26,6 @@ makeTUDiagnostics(const std::string &Mai
TUs.push_back({MainSourceFile,
{{DiagnosticName,
Message,
- Replacements,
{},
Diagnostic::Warning,
BuildDirectory}}});
Modified: clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h?rev=358576&r1=358575&r2=358576&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h (original)
+++ clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h Wed Apr 17 05:53:59 2019
@@ -14,6 +14,8 @@
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
+#include "clang/Tooling/Core/Diagnostic.h"
+#include "clang/Tooling/Core/Replacement.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/Optional.h"
@@ -130,16 +132,17 @@ runCheckOnCode(StringRef Code, std::vect
tooling::Replacements Fixes;
std::vector<ClangTidyError> Diags = DiagConsumer.take();
for (const ClangTidyError &Error : Diags) {
- for (const auto &FileAndFixes : Error.Fix) {
- for (const auto &Fix : FileAndFixes.second) {
- auto Err = Fixes.add(Fix);
- // FIXME: better error handling. Keep the behavior for now.
- if (Err) {
- llvm::errs() << llvm::toString(std::move(Err)) << "\n";
- return "";
+ if (const auto *ChosenFix = tooling::selectFirstFix(Error))
+ for (const auto &FileAndFixes : *ChosenFix) {
+ for (const auto &Fix : FileAndFixes.second) {
+ auto Err = Fixes.add(Fix);
+ // FIXME: better error handling. Keep the behavior for now.
+ if (Err) {
+ llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+ return "";
+ }
}
}
- }
}
if (Errors)
*Errors = std::move(Diags);
More information about the cfe-commits
mailing list