[clang-tools-extra] 922a5b8 - [clang-tidy] Add test for Transformer-based checks with diagnostics.
Yitzhak Mandelbaum via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 12 12:16:12 PST 2021
Author: Yitzhak Mandelbaum
Date: 2021-01-12T20:15:22Z
New Revision: 922a5b894114defb5302e514973de8c9cd23af6a
URL: https://github.com/llvm/llvm-project/commit/922a5b894114defb5302e514973de8c9cd23af6a
DIFF: https://github.com/llvm/llvm-project/commit/922a5b894114defb5302e514973de8c9cd23af6a.diff
LOG: [clang-tidy] Add test for Transformer-based checks with diagnostics.
Adds a test that checks the diagnostic output of the tidy.
Differential Revision: https://reviews.llvm.org/D94453
Added:
Modified:
clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp b/clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
index e8df4bb60071..24b6bea98787 100644
--- a/clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
@@ -10,8 +10,10 @@
#include "ClangTidyTest.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/Transformer/RangeSelector.h"
+#include "clang/Tooling/Transformer/RewriteRule.h"
#include "clang/Tooling/Transformer/Stencil.h"
#include "clang/Tooling/Transformer/Transformer.h"
+#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
@@ -25,20 +27,21 @@ using transformer::change;
using transformer::IncludeFormat;
using transformer::makeRule;
using transformer::node;
+using transformer::noopEdit;
using transformer::RewriteRule;
+using transformer::RootID;
using transformer::statement;
// Invert the code of an if-statement, while maintaining its semantics.
RewriteRule invertIf() {
StringRef C = "C", T = "T", E = "E";
- RewriteRule Rule =
- makeRule(ifStmt(hasCondition(expr().bind(C)), hasThen(stmt().bind(T)),
- hasElse(stmt().bind(E))),
- change(statement(std::string(RewriteRule::RootID)),
- cat("if(!(", node(std::string(C)), ")) ",
- statement(std::string(E)), " else ",
- statement(std::string(T)))),
- cat("negate condition and reverse `then` and `else` branches"));
+ RewriteRule Rule = makeRule(
+ ifStmt(hasCondition(expr().bind(C)), hasThen(stmt().bind(T)),
+ hasElse(stmt().bind(E))),
+ change(statement(RootID), cat("if(!(", node(std::string(C)), ")) ",
+ statement(std::string(E)), " else ",
+ statement(std::string(T)))),
+ cat("negate condition and reverse `then` and `else` branches"));
return Rule;
}
@@ -68,6 +71,25 @@ TEST(TransformerClangTidyCheckTest, Basic) {
EXPECT_EQ(Expected, test::runCheckOnCode<IfInverterCheck>(Input));
}
+TEST(TransformerClangTidyCheckTest, DiagnosticsCorrectlyGenerated) {
+ class DiagOnlyCheck : public TransformerClangTidyCheck {
+ public:
+ DiagOnlyCheck(StringRef Name, ClangTidyContext *Context)
+ : TransformerClangTidyCheck(
+ makeRule(returnStmt(), noopEdit(node(RootID)), cat("message")),
+ Name, Context) {}
+ };
+ std::string Input = "int h() { return 5; }";
+ std::vector<ClangTidyError> Errors;
+ EXPECT_EQ(Input, test::runCheckOnCode<DiagOnlyCheck>(Input, &Errors));
+ EXPECT_EQ(Errors.size(), 1U);
+ EXPECT_EQ(Errors[0].Message.Message, "message");
+ EXPECT_THAT(Errors[0].Ranges, testing::IsEmpty());
+
+ // The diagnostic is anchored to the match, "return 5".
+ EXPECT_EQ(Errors[0].Message.FileOffset, 10U);
+}
+
class IntLitCheck : public TransformerClangTidyCheck {
public:
IntLitCheck(StringRef Name, ClangTidyContext *Context)
More information about the cfe-commits
mailing list