[clang-tools-extra] 3ed40ac - [clang-tidy] Fixed an issue in readability-identifier-naming not using options specified.
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 18 08:58:42 PDT 2023
Author: Nathan James
Date: 2023-03-18T15:54:25Z
New Revision: 3ed40ac90ce8a91f0990fe2d847feb6663606f93
URL: https://github.com/llvm/llvm-project/commit/3ed40ac90ce8a91f0990fe2d847feb6663606f93
DIFF: https://github.com/llvm/llvm-project/commit/3ed40ac90ce8a91f0990fe2d847feb6663606f93.diff
LOG: [clang-tidy] Fixed an issue in readability-identifier-naming not using options specified.
Fixed an issue where specifying empty strings for prefix or suffix would be ignored preventing using those to override a different style.
Fixes https://github.com/llvm/llvm-project/issues/56358.
Reviewed By: carlosgalvezp
Differential Revision: https://reviews.llvm.org/D129070
Added:
clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-empty-options.cpp
Modified:
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
clang-tools-extra/docs/ReleaseNotes.rst
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index 61421a13d73ba..9b2b9f7672f4d 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -230,9 +230,8 @@ static StringRef const HungarainNotationUserDefinedTypes[] = {
// clang-format on
IdentifierNamingCheck::NamingStyle::NamingStyle(
- std::optional<IdentifierNamingCheck::CaseType> Case,
- const std::string &Prefix, const std::string &Suffix,
- const std::string &IgnoredRegexpStr, HungarianPrefixType HPType)
+ std::optional<IdentifierNamingCheck::CaseType> Case, StringRef Prefix,
+ StringRef Suffix, StringRef IgnoredRegexpStr, HungarianPrefixType HPType)
: Case(Case), Prefix(Prefix), Suffix(Suffix),
IgnoredRegexpStr(IgnoredRegexpStr), HPType(HPType) {
if (!IgnoredRegexpStr.empty()) {
@@ -265,22 +264,21 @@ IdentifierNamingCheck::FileStyle IdentifierNamingCheck::getFileStyleFromOptions(
memcpy(&StyleString[StyleSize], "IgnoredRegexp", 13);
StyleString.truncate(StyleSize + 13);
- StringRef IgnoredRegexpStr = Options.get(StyleString, "");
+ std::optional<StringRef> IgnoredRegexpStr = Options.get(StyleString);
memcpy(&StyleString[StyleSize], "Prefix", 6);
StyleString.truncate(StyleSize + 6);
- std::string Prefix(Options.get(StyleString, ""));
+ std::optional<StringRef> Prefix(Options.get(StyleString));
// Fast replacement of [Pre]fix -> [Suf]fix.
memcpy(&StyleString[StyleSize], "Suf", 3);
- std::string Postfix(Options.get(StyleString, ""));
+ std::optional<StringRef> Postfix(Options.get(StyleString));
memcpy(&StyleString[StyleSize], "Case", 4);
StyleString.pop_back_n(2);
- auto CaseOptional =
+ std::optional<CaseType> CaseOptional =
Options.get<IdentifierNamingCheck::CaseType>(StyleString);
- if (CaseOptional || !Prefix.empty() || !Postfix.empty() ||
- !IgnoredRegexpStr.empty() || HPTOpt)
- Styles[I].emplace(std::move(CaseOptional), std::move(Prefix),
- std::move(Postfix), IgnoredRegexpStr.str(),
+ if (CaseOptional || Prefix || Postfix || IgnoredRegexpStr || HPTOpt)
+ Styles[I].emplace(std::move(CaseOptional), Prefix.value_or(""),
+ Postfix.value_or(""), IgnoredRegexpStr.value_or(""),
HPTOpt.value_or(IdentifierNamingCheck::HPT_Off));
}
bool IgnoreMainLike = Options.get("IgnoreMainLikeFunctions", false);
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
index cbf0653a280b6..b1db919902e22 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
@@ -68,8 +68,8 @@ class IdentifierNamingCheck final : public RenamerClangTidyCheck {
struct NamingStyle {
NamingStyle() = default;
- NamingStyle(std::optional<CaseType> Case, const std::string &Prefix,
- const std::string &Suffix, const std::string &IgnoredRegexpStr,
+ NamingStyle(std::optional<CaseType> Case, StringRef Prefix,
+ StringRef Suffix, StringRef IgnoredRegexpStr,
HungarianPrefixType HPType);
NamingStyle(const NamingStyle &O) = delete;
NamingStyle &operator=(NamingStyle &&O) = default;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index b53516a742e2c..e1bbe6776f1e8 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -223,6 +223,11 @@ Changes in existing checks
<clang-tidy/checks/bugprone/use-after-move>` to understand that there is a
sequence point between designated initializers.
+- Fixed an issue in :doc:`readability-identifier-naming
+ <clang-tidy/checks/readability/identifier-naming>` when specifying an empty
+ string for ``Prefix`` or ``Suffix`` options could result in the style not
+ being used.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-empty-options.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-empty-options.cpp
new file mode 100644
index 0000000000000..fa261d8bc377e
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-empty-options.cpp
@@ -0,0 +1,12 @@
+// RUN: %check_clang_tidy %s readability-identifier-naming %t -- \
+// RUN: -config='{CheckOptions: { \
+// RUN: readability-identifier-naming.GlobalConstantPrefix: "", \
+// RUN: readability-identifier-naming.GlobalVariablePrefix: g_ }}'
+
+int BadGlobalVariable;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for global variable 'BadGlobalVariable' [readability-identifier-naming]
+// CHECK-FIXES: int g_BadGlobalVariable;
+int g_GoodGlobalVariable;
+
+const int GoodGlobalConstant = 0;
+const int g_IgnoreGlobalConstant = 0;
More information about the cfe-commits
mailing list