diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt @@ -5,6 +5,7 @@ add_clang_library(clangTidyMiscModule DefinitionsInHeadersCheck.cpp + MatchObjcStringLiteralCheck.cpp MiscTidyModule.cpp MisleadingBidirectional.cpp MisleadingIdentifier.cpp diff --git a/clang-tools-extra/clang-tidy/misc/MatchObjcStringLiteralCheck.h b/clang-tools-extra/clang-tidy/misc/MatchObjcStringLiteralCheck.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/misc/MatchObjcStringLiteralCheck.h @@ -0,0 +1,37 @@ +//===--- MatchObjcStringLiteralCheck.h - clang-tidy -------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MATCHOBJCSTRINGLITERALCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MATCHOBJCSTRINGLITERALCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace misc { + +/// Matches all ObjCStringLiteral types. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/match-objc-string-literal.html +class MatchObjcStringLiteralCheck : public ClangTidyCheck { +public: + MatchObjcStringLiteralCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.ObjC; + } + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace misc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MATCHOBJCSTRINGLITERALCHECK_H diff --git a/clang-tools-extra/clang-tidy/misc/MatchObjcStringLiteralCheck.cpp b/clang-tools-extra/clang-tidy/misc/MatchObjcStringLiteralCheck.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/misc/MatchObjcStringLiteralCheck.cpp @@ -0,0 +1,32 @@ +//===--- MatchObjcStringLiteralCheck.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 "MatchObjcStringLiteralCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace misc { + +void MatchObjcStringLiteralCheck::registerMatchers(MatchFinder *Finder) { + // Adding matchers. + Finder->addMatcher(objCStringLiteral().bind("str_lit"), this); +} + +void MatchObjcStringLiteralCheck::check(const MatchFinder::MatchResult &Result) { + const auto *StrExpr = Result.Nodes.getNodeAs("str_lit"); + const StringLiteral* SL = cast(StrExpr)->getString(); + diag(StrExpr->getExprLoc(), "Matched ObjC StringLiteral: %0") << SL->getString(); +} + +} +} +} diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp --- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "DefinitionsInHeadersCheck.h" +#include "MatchObjcStringLiteralCheck.h" #include "MisleadingBidirectional.h" #include "MisleadingIdentifier.h" #include "MisplacedConstCheck.h" @@ -35,6 +36,8 @@ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck( "misc-definitions-in-headers"); + CheckFactories.registerCheck( + "misc-match-objc-string-literal"); CheckFactories.registerCheck( "misc-misleading-bidirectional"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -123,6 +123,11 @@ Warns when the code is unwrapping a `std::optional`, `absl::optional`, or `base::Optional` object without assuring that it contains a value. +- New :doc:`misc-match-objc-string-literal + ` check. + + Matches ObjCStringLiteral type. + - New :doc:`modernize-macro-to-enum ` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/match-objc-string-literal.rst b/clang-tools-extra/docs/clang-tidy/checks/misc/match-objc-string-literal.rst new file mode 100644 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/misc/match-objc-string-literal.rst @@ -0,0 +1,6 @@ +.. title:: clang-tidy - misc-match-objc-string-literal + +misc-match-objc-string-literal +============================== + +FIXME: Matches all ObjCStringLiteral types. diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc-match-objc-string-literal.m b/clang-tools-extra/test/clang-tidy/checkers/misc-match-objc-string-literal.m new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/misc-match-objc-string-literal.m @@ -0,0 +1,19 @@ +// RUN: %check_clang_tidy %s misc-match-objc-string-literal %t +@interface NSObject +@end + +@interface NSString +@end + +@interface Test : NSObject ++ (void)someFunction:(NSString *)Desc; +@end + +@implementation Test ++ (void)someFunction:(NSString *)Desc { + return; +} +- (void) foo { + [Test someFunction:@"Ola!"]; +} +@end diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -1515,6 +1515,9 @@ extern const internal::VariadicDynCastAllOfMatcher objcMessageExpr; +extern const internal::VariadicDynCastAllOfMatcher + objCStringLiteral; + /// Matches Objective-C interface declarations. /// /// Example matches Foo diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp --- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -917,6 +917,7 @@ const internal::VariadicDynCastAllOfMatcher cxxBoolLiteral; const internal::VariadicDynCastAllOfMatcher stringLiteral; +const internal::VariadicDynCastAllOfMatcher objCStringLiteral; const internal::VariadicDynCastAllOfMatcher characterLiteral; const internal::VariadicDynCastAllOfMatcher diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp --- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/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);