t-rasmud updated this revision to Diff 438852.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128103/new/

https://reviews.llvm.org/D128103

Files:
  clang-tools-extra/unittests/clang-tidy/CMakeLists.txt
  clang-tools-extra/unittests/clang-tidy/ObjCStringLiteralTest.cpp
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp

Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -548,6 +548,7 @@
   REGISTER_MATCHER(stmt);
   REGISTER_MATCHER(stmtExpr);
   REGISTER_MATCHER(stringLiteral);
+  REGISTER_MATCHER(objCStringLiteral);
   REGISTER_MATCHER(substNonTypeTemplateParmExpr);
   REGISTER_MATCHER(substTemplateTypeParmType);
   REGISTER_MATCHER(switchCase);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===================================================================
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -917,6 +917,7 @@
 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBoolLiteralExpr>
     cxxBoolLiteral;
 const internal::VariadicDynCastAllOfMatcher<Stmt, StringLiteral> stringLiteral;
+const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCStringLiteral> objCStringLiteral;
 const internal::VariadicDynCastAllOfMatcher<Stmt, CharacterLiteral>
     characterLiteral;
 const internal::VariadicDynCastAllOfMatcher<Stmt, IntegerLiteral>
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1515,6 +1515,9 @@
 extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCMessageExpr>
     objcMessageExpr;
 
+extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCStringLiteral>
+    objCStringLiteral;
+
 /// Matches Objective-C interface declarations.
 ///
 /// Example matches Foo
Index: clang-tools-extra/unittests/clang-tidy/ObjCStringLiteralTest.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/unittests/clang-tidy/ObjCStringLiteralTest.cpp
@@ -0,0 +1,63 @@
+//===---- ObjCStringLiteralTest.cpp - clang-tidy ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "ClangTidyTest.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace tidy {
+namespace test {
+namespace {
+
+class ObjCStringLiteralCheck : public ClangTidyCheck {
+public:
+    ObjCStringLiteralCheck(StringRef CheckName, ClangTidyContext *Context)
+      : ClangTidyCheck(CheckName, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override {
+    using namespace ast_matchers;
+      Finder->addMatcher(objCStringLiteral().bind("str_lit"), this);
+  }
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
+      const auto *StrExpr = Result.Nodes.getNodeAs<ObjCStringLiteral>("str_lit");
+      const StringLiteral* SL = cast<ObjCStringLiteral>(StrExpr)->getString();
+      diag(StrExpr->getExprLoc(), "Matched ObjC StringLiteral: %0") << SL->getString();
+  }
+};
+
+} // namespace
+
+TEST(ObjCStringLiteralTest, ObjCStringLiteralCheck) {
+  std::vector<ClangTidyError> Errors;
+  runCheckOnCode<ObjCStringLiteralCheck>(
+      "@interface NSObject\n"
+      "@end\n"
+      "@interface NSString\n"
+      "@end\n"
+      "@interface Test : NSObject\n"
+      "+ (void)someFunction:(NSString *)Desc;\n"
+      "@end\n"
+      "@implementation Test\n"
+      "+ (void)someFunction:(NSString *)Desc {\n"
+      "    return;\n"
+      "}\n"
+      "- (void) foo {\n"
+      "    [Test someFunction:@\"Ola!\"];\n"
+      "}\n"
+      "@end\n",
+      &Errors,
+      "input.m");
+  EXPECT_EQ(1ul, Errors.size());
+  EXPECT_EQ(
+      "Matched ObjC StringLiteral: Ola!",
+      Errors[0].Message.Message);
+}
+
+} // namespace test
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/unittests/clang-tidy/CMakeLists.txt
===================================================================
--- clang-tools-extra/unittests/clang-tidy/CMakeLists.txt
+++ clang-tools-extra/unittests/clang-tidy/CMakeLists.txt
@@ -28,6 +28,7 @@
   ModernizeModuleTest.cpp
   NamespaceAliaserTest.cpp
   ObjCModuleTest.cpp
+  ObjCStringLiteralTest.cpp
   OptionsProviderTest.cpp
   OverlappingReplacementsTest.cpp
   UsingInserterTest.cpp
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to