[clang-tools-extra] r323130 - [clang-tidy objc-property-declaration] New option IncludeDefaultAcronyms
Ben Hamilton via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 22 07:45:25 PST 2018
Author: benhamilton
Date: Mon Jan 22 07:45:25 2018
New Revision: 323130
URL: http://llvm.org/viewvc/llvm-project?rev=323130&view=rev
Log:
[clang-tidy objc-property-declaration] New option IncludeDefaultAcronyms
Summary:
The existing option objc-property-declaration.Acronyms
replaces the built-in set of acronyms.
While this behavior is OK for clients that don't want the default
behavior, many clients may just want to add their own custom acronyms
to the default list.
This revision introduces a new option,
objc-property-declaration.IncludeDefaultAcronyms, which controls
whether the acronyms in objc-property-declaration.Acronyms are
appended to the default list (the default behavior) or whether they
replace.
I also updated the documentation.
Test Plan: make -j12 check-clang-tools
Reviewers: Wizard, hokein, klimek
Reviewed By: hokein
Subscribers: Eugene.Zelenko, cfe-commits
Differential Revision: https://reviews.llvm.org/D42261
Added:
clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-additional.m
- copied, changed from r323112, clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-custom.m
Modified:
clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.cpp
clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/objc-property-declaration.rst
clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-custom.m
Modified: clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.cpp?rev=323130&r1=323129&r2=323130&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.cpp Mon Jan 22 07:45:25 2018
@@ -11,6 +11,7 @@
#include "../utils/OptionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Regex.h"
#include <algorithm>
@@ -26,61 +27,62 @@ namespace {
/// https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/APIAbbreviations.html#//apple_ref/doc/uid/20001285-BCIHCGAE
///
/// Keep this list sorted.
-constexpr char DefaultSpecialAcronyms[] =
- "ACL;"
- "API;"
- "ARGB;"
- "ASCII;"
- "BGRA;"
- "CMYK;"
- "DNS;"
- "FPS;"
- "FTP;"
- "GIF;"
- "GPS;"
- "HD;"
- "HDR;"
- "HTML;"
- "HTTP;"
- "HTTPS;"
- "HUD;"
- "ID;"
- "JPG;"
- "JS;"
- "LAN;"
- "LZW;"
- "MDNS;"
- "MIDI;"
- "OS;"
- "PDF;"
- "PIN;"
- "PNG;"
- "POI;"
- "PSTN;"
- "PTR;"
- "QA;"
- "QOS;"
- "RGB;"
- "RGBA;"
- "RGBX;"
- "ROM;"
- "RPC;"
- "RTF;"
- "RTL;"
- "SDK;"
- "SSO;"
- "TCP;"
- "TIFF;"
- "TTS;"
- "UI;"
- "URI;"
- "URL;"
- "VC;"
- "VOIP;"
- "VPN;"
- "VR;"
- "WAN;"
- "XML";
+constexpr llvm::StringLiteral DefaultSpecialAcronyms[] = {
+ "ACL",
+ "API",
+ "ARGB",
+ "ASCII",
+ "BGRA",
+ "CMYK",
+ "DNS",
+ "FPS",
+ "FTP",
+ "GIF",
+ "GPS",
+ "HD",
+ "HDR",
+ "HTML",
+ "HTTP",
+ "HTTPS",
+ "HUD",
+ "ID",
+ "JPG",
+ "JS",
+ "LAN",
+ "LZW",
+ "MDNS",
+ "MIDI",
+ "OS",
+ "PDF",
+ "PIN",
+ "PNG",
+ "POI",
+ "PSTN",
+ "PTR",
+ "QA",
+ "QOS",
+ "RGB",
+ "RGBA",
+ "RGBX",
+ "ROM",
+ "RPC",
+ "RTF",
+ "RTL",
+ "SDK",
+ "SSO",
+ "TCP",
+ "TIFF",
+ "TTS",
+ "UI",
+ "URI",
+ "URL",
+ "VC",
+ "VOIP",
+ "VPN",
+ "VR",
+ "WAN",
+ "XML",
+};
/// For now we will only fix 'CamelCase' property to
/// 'camelCase'. For other cases the users need to
@@ -97,14 +99,7 @@ FixItHint generateFixItHint(const ObjCPr
return FixItHint();
}
-std::string validPropertyNameRegex(const std::vector<std::string> &Acronyms) {
- std::vector<std::string> EscapedAcronyms;
- EscapedAcronyms.reserve(Acronyms.size());
- // In case someone defines a custom prefix which includes a regex
- // special character, escape all the prefixes.
- std::transform(Acronyms.begin(), Acronyms.end(),
- std::back_inserter(EscapedAcronyms), [](const std::string& s) {
- return llvm::Regex::escape(s); });
+std::string validPropertyNameRegex(const std::vector<std::string> &EscapedAcronyms) {
// Allow any of these names:
// foo
// fooBar
@@ -123,15 +118,32 @@ std::string validPropertyNameRegex(const
PropertyDeclarationCheck::PropertyDeclarationCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- SpecialAcronyms(utils::options::parseStringList(
- Options.get("Acronyms", DefaultSpecialAcronyms))) {}
+ SpecialAcronyms(
+ utils::options::parseStringList(Options.get("Acronyms", ""))),
+ IncludeDefaultAcronyms(Options.get("IncludeDefaultAcronyms", true)) {}
void PropertyDeclarationCheck::registerMatchers(MatchFinder *Finder) {
+ std::vector<std::string> EscapedAcronyms;
+ if (IncludeDefaultAcronyms) {
+ EscapedAcronyms.reserve(llvm::array_lengthof(DefaultSpecialAcronyms) +
+ SpecialAcronyms.size());
+ // No need to regex-escape the default acronyms.
+ EscapedAcronyms.insert(EscapedAcronyms.end(),
+ std::begin(DefaultSpecialAcronyms),
+ std::end(DefaultSpecialAcronyms));
+ } else {
+ EscapedAcronyms.reserve(SpecialAcronyms.size());
+ }
+ // In case someone defines a prefix which includes a regex
+ // special character, regex-escape all the user-defined prefixes.
+ std::transform(SpecialAcronyms.begin(), SpecialAcronyms.end(),
+ std::back_inserter(EscapedAcronyms),
+ [](const std::string &s) { return llvm::Regex::escape(s); });
Finder->addMatcher(
objcPropertyDecl(
// the property name should be in Lower Camel Case like
// 'lowerCamelCase'
- unless(matchesName(validPropertyNameRegex(SpecialAcronyms))))
+ unless(matchesName(validPropertyNameRegex(EscapedAcronyms))))
.bind("property"),
this);
}
@@ -149,6 +161,7 @@ void PropertyDeclarationCheck::check(con
void PropertyDeclarationCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "Acronyms",
utils::options::serializeStringList(SpecialAcronyms));
+ Options.store(Opts, "IncludeDefaultAcronyms", IncludeDefaultAcronyms);
}
} // namespace objc
Modified: clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.h?rev=323130&r1=323129&r2=323130&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/objc/PropertyDeclarationCheck.h Mon Jan 22 07:45:25 2018
@@ -34,7 +34,8 @@ public:
void storeOptions(ClangTidyOptions::OptionMap &Options) override;
private:
- const std::vector<std::string> SpecialAcronyms;
+ const std::vector<std::string> SpecialAcronyms;
+ const bool IncludeDefaultAcronyms;
};
} // namespace objc
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/objc-property-declaration.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/objc-property-declaration.rst?rev=323130&r1=323129&r2=323130&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/objc-property-declaration.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/objc-property-declaration.rst Mon Jan 22 07:45:25 2018
@@ -37,7 +37,25 @@ Options
.. option:: Acronyms
- Semicolon-separated list of acronyms that can be used as a prefix
+ Semicolon-separated list of custom acronyms that can be used as a prefix
or a suffix of property names.
- If unset, defaults to "ACL;API;ARGB;ASCII;BGRA;CMYK;DNS;FPS;FTP;GIF;GPS;HD;HDR;HTML;HTTP;HTTPS;HUD;ID;JPG;JS;LAN;LZW;MDNS;MIDI;OS;PDF;PIN;PNG;POI;PSTN;PTR;QA;QOS;RGB;RGBA;RGBX;ROM;RPC;RTF;RTL;SDK;SSO;TCP;TIFF;TTS;UI;URI;URL;VC;VOIP;VPN;VR;WAN;XML".
+ By default, appends to the list of default acronyms (
+ ``IncludeDefaultAcronyms`` set to ``1``).
+ If ``IncludeDefaultAcronyms`` is set to ``0``, instead replaces the
+ default list of acronyms.
+
+.. option:: IncludeDefaultAcronyms
+
+ Integer value (defaults to ``1``) to control whether the default
+ acronyms are included in the list of acronyms.
+
+ If set to ``1``, the value in ``Acronyms`` is appended to the
+ default list of acronyms:
+
+ ``ACL;API;ARGB;ASCII;BGRA;CMYK;DNS;FPS;FTP;GIF;GPS;HD;HDR;HTML;HTTP;HTTPS;
+HUD;ID;JPG;JS;LAN;LZW;MDNS;MIDI;OS;PDF;PIN;PNG;POI;PSTN;PTR;QA;QOS;RGB;RGBA;
+RGBX;ROM;RPC;RTF;RTL;SDK;SSO;TCP;TIFF;TTS;UI;URI;URL;VC;VOIP;VPN;VR;WAN;XML``.
+
+ If set to ``0``, the value in ``Acronyms`` replaces the default list
+ of acronyms.
Copied: clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-additional.m (from r323112, clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-custom.m)
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-additional.m?p2=clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-additional.m&p1=clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-custom.m&r1=323112&r2=323130&rev=323130&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-custom.m (original)
+++ clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-additional.m Mon Jan 22 07:45:25 2018
@@ -11,4 +11,5 @@
@property(assign, nonatomic) int ABCCustomPrefix;
@property(strong, nonatomic) NSString *ABC_custom_prefix;
// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: property name 'ABC_custom_prefix' should use lowerCamelCase style, according to the Apple Coding Guidelines [objc-property-declaration]
+ at property(assign, nonatomic) int GIFShouldIncludeStandardAcronym;
@end
Modified: clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-custom.m
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-custom.m?rev=323130&r1=323129&r2=323130&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-custom.m (original)
+++ clang-tools-extra/trunk/test/clang-tidy/objc-property-declaration-custom.m Mon Jan 22 07:45:25 2018
@@ -1,6 +1,7 @@
// RUN: %check_clang_tidy %s objc-property-declaration %t \
// RUN: -config='{CheckOptions: \
-// RUN: [{key: objc-property-declaration.Acronyms, value: "ABC;TGIF"}]}' \
+// RUN: [{key: objc-property-declaration.Acronyms, value: "ABC;TGIF"}, \
+// RUN: {key: objc-property-declaration.IncludeDefaultAcronyms, value: 0}]}' \
// RUN: --
@class NSString;
@@ -11,4 +12,6 @@
@property(assign, nonatomic) int ABCCustomPrefix;
@property(strong, nonatomic) NSString *ABC_custom_prefix;
// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: property name 'ABC_custom_prefix' should use lowerCamelCase style, according to the Apple Coding Guidelines [objc-property-declaration]
+ at property(assign, nonatomic) int GIFIgnoreStandardAcronym;
+// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: property name 'GIFIgnoreStandardAcronym' should use lowerCamelCase style, according to the Apple Coding Guidelines [objc-property-declaration]
@end
More information about the cfe-commits
mailing list