Skip to content

Cherrypicks NSDateFormatter checker and other related (missing) commits in clang-tidy #5075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0ded456
[clang-tidy] Confusable identifiers detection
Oct 15, 2021
a8c68f0
[clang-tidy] Fix confusable identifiers interaction with DeclContext
Jun 28, 2022
547980d
Adds AST matcher for ObjCStringLiteral
t-rasmud Jun 29, 2022
c3c9312
Adds the NSDateFormatter checker to clang-tidy
t-rasmud Aug 2, 2022
ab10511
[clang-tidy] Organize test files into subdirectories by module (NFC)
LegalizeAdulthood Jun 17, 2022
f8f21bb
Don't treat invalid parameters as being unused
AaronBallman Jun 22, 2022
791d349
[clang-tidy] Support expressions of literals in modernize-macro-to-enum
LegalizeAdulthood Apr 23, 2022
d5975d0
[clang-tidy] Ignore macros defined within declarations
LegalizeAdulthood Apr 17, 2022
74c4513
[clang-tidy][NFC] Replace many instances of std::string where a Strin…
njames93 May 9, 2022
eaddd5d
[clang-tidy] Fix unintended change left in 12cb540529e
njames93 May 10, 2022
c617003
[clang-tidy][NFC] Reimplement SimplifyBooleanExpr with RecursiveASTVi…
njames93 May 16, 2022
bf5d7e4
[clang-tidy] Fix readability-simplify-boolean-expr crash with implici…
njames93 May 18, 2022
85fd4d1
[clang-tidy] Fix readability-simplify-boolean-expr when Ifs have an i…
njames93 May 18, 2022
285aaeb
[clang-tidy] Fix logic of assertion
sam-mccall May 19, 2022
19f9135
[clang-tidy] add support for Demorgan conversions to readability-simp…
njames93 May 22, 2022
fe0a7aa
[clang-tidy] Fix not updating storeOptions after af77b1d9901
njames93 May 22, 2022
fd70441
[clang-tidy] Extend SimplifyBooleanExpr demorgan support.
njames93 May 25, 2022
146c01b
[clang-tidy][NFC] Tweak identifier-naming options reading/writiing
njames93 Jun 10, 2022
50d6c3d
[clang-tidy] Organize check doc files into subdirectories (NFC)
LegalizeAdulthood May 18, 2022
a03c7af
[clang-tidy] modernize-deprecated-headers check should respect extern…
May 13, 2022
4666d2a
Revert "[clang-tidy] modernize-deprecated-headers check should respec…
May 13, 2022
304826a
Reland "[clang-tidy] modernize-deprecated-headers check should respec…
May 20, 2022
f319dcc
[clang-tidy] Add proper emplace checks to modernize-use-emplace
nicovank Jun 2, 2022
ae5efce
[clang-tidy] Reject invalid enum initializers in C files
LegalizeAdulthood May 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
38 changes: 17 additions & 21 deletions clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) {
ClangTidyCheck::OptionsView::OptionsView(
StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions,
ClangTidyContext *Context)
: NamePrefix(CheckName.str() + "."), CheckOptions(CheckOptions),
: NamePrefix((CheckName + ".").str()), CheckOptions(CheckOptions),
Context(Context) {}

llvm::Optional<std::string>
llvm::Optional<StringRef>
ClangTidyCheck::OptionsView::get(StringRef LocalName) const {
const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
const auto &Iter = CheckOptions.find((NamePrefix + LocalName).str());
if (Iter != CheckOptions.end())
return Iter->getValue().Value;
return StringRef(Iter->getValue().Value);
return None;
}

static ClangTidyOptions::OptionMap::const_iterator
findPriorityOption(const ClangTidyOptions::OptionMap &Options, StringRef NamePrefix,
StringRef LocalName) {
auto IterLocal = Options.find((NamePrefix + LocalName).str());
auto IterGlobal = Options.find(LocalName.str());
auto IterGlobal = Options.find(LocalName);
if (IterLocal == Options.end())
return IterGlobal;
if (IterGlobal == Options.end())
Expand All @@ -73,11 +73,11 @@ findPriorityOption(const ClangTidyOptions::OptionMap &Options, StringRef NamePre
return IterGlobal;
}

llvm::Optional<std::string>
llvm::Optional<StringRef>
ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName) const {
auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName);
if (Iter != CheckOptions.end())
return Iter->getValue().Value;
return StringRef(Iter->getValue().Value);
return None;
}

Expand All @@ -97,7 +97,7 @@ static Optional<bool> getAsBool(StringRef Value,
template <>
llvm::Optional<bool>
ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName) const {
if (llvm::Optional<std::string> ValueOr = get(LocalName)) {
if (llvm::Optional<StringRef> ValueOr = get(LocalName)) {
if (auto Result = getAsBool(*ValueOr, NamePrefix + LocalName))
return Result;
diagnoseBadBooleanOption(NamePrefix + LocalName, *ValueOr);
Expand All @@ -120,7 +120,7 @@ ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName) const {
void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
StringRef LocalName,
StringRef Value) const {
Options[NamePrefix + LocalName.str()] = Value;
Options[(NamePrefix + LocalName).str()] = Value;
}

void ClangTidyCheck::OptionsView::storeInt(ClangTidyOptions::OptionMap &Options,
Expand Down Expand Up @@ -167,10 +167,9 @@ llvm::Optional<int64_t> ClangTidyCheck::OptionsView::getEnumInt(
}
}
if (EditDistance < 3)
diagnoseBadEnumOption(Iter->getKey().str(), Iter->getValue().Value,
Closest);
diagnoseBadEnumOption(Iter->getKey(), Iter->getValue().Value, Closest);
else
diagnoseBadEnumOption(Iter->getKey().str(), Iter->getValue().Value);
diagnoseBadEnumOption(Iter->getKey(), Iter->getValue().Value);
return None;
}

Expand Down Expand Up @@ -203,18 +202,15 @@ void ClangTidyCheck::OptionsView::diagnoseBadEnumOption(
Diag << 3 << Suggestion;
}

std::string ClangTidyCheck::OptionsView::get(StringRef LocalName,
StringRef Default) const {
if (llvm::Optional<std::string> Val = get(LocalName))
return std::move(*Val);
return Default.str();
StringRef ClangTidyCheck::OptionsView::get(StringRef LocalName,
StringRef Default) const {
return get(LocalName).getValueOr(Default);
}
std::string

StringRef
ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName,
StringRef Default) const {
if (llvm::Optional<std::string> Val = getLocalOrGlobal(LocalName))
return std::move(*Val);
return Default.str();
return getLocalOrGlobal(LocalName).getValueOr(Default);
}
} // namespace tidy
} // namespace clang
12 changes: 6 additions & 6 deletions clang-tools-extra/clang-tidy/ClangTidyCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,30 +155,30 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
/// Reads the option with the check-local name \p LocalName from the
/// ``CheckOptions``. If the corresponding key is not present, return
/// ``None``.
llvm::Optional<std::string> get(StringRef LocalName) const;
llvm::Optional<StringRef> get(StringRef LocalName) const;

/// Read a named option from the ``Context``.
///
/// Reads the option with the check-local name \p LocalName from the
/// ``CheckOptions``. If the corresponding key is not present, returns
/// \p Default.
std::string get(StringRef LocalName, StringRef Default) const;
StringRef get(StringRef LocalName, StringRef Default) const;

/// Read a named option from the ``Context``.
///
/// Reads the option with the check-local name \p LocalName from local or
/// global ``CheckOptions``. Gets local option first. If local is not
/// present, falls back to get global option. If global option is not
/// present either, return ``None``.
llvm::Optional<std::string> getLocalOrGlobal(StringRef LocalName) const;
llvm::Optional<StringRef> getLocalOrGlobal(StringRef LocalName) const;

/// Read a named option from the ``Context``.
///
/// Reads the option with the check-local name \p LocalName from local or
/// global ``CheckOptions``. Gets local option first. If local is not
/// present, falls back to get global option. If global option is not
/// present either, returns \p Default.
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const;
StringRef getLocalOrGlobal(StringRef LocalName, StringRef Default) const;

/// Read a named option from the ``Context`` and parse it as an
/// integral type ``T``.
Expand All @@ -192,7 +192,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
template <typename T>
std::enable_if_t<std::is_integral<T>::value, llvm::Optional<T>>
get(StringRef LocalName) const {
if (llvm::Optional<std::string> Value = get(LocalName)) {
if (llvm::Optional<StringRef> Value = get(LocalName)) {
T Result{};
if (!StringRef(*Value).getAsInteger(10, Result))
return Result;
Expand Down Expand Up @@ -229,7 +229,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
template <typename T>
std::enable_if_t<std::is_integral<T>::value, llvm::Optional<T>>
getLocalOrGlobal(StringRef LocalName) const {
llvm::Optional<std::string> ValueOr = get(LocalName);
llvm::Optional<StringRef> ValueOr = get(LocalName);
bool IsGlobal = false;
if (!ValueOr) {
IsGlobal = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name,

void StringFindStartswithCheck::registerMatchers(MatchFinder *Finder) {
auto ZeroLiteral = integerLiteral(equals(0));
auto StringClassMatcher = cxxRecordDecl(hasAnyName(SmallVector<StringRef, 4>(
StringLikeClasses.begin(), StringLikeClasses.end())));
auto StringClassMatcher = cxxRecordDecl(hasAnyName(StringLikeClasses));
auto StringType = hasUnqualifiedDesugaredType(
recordType(hasDeclaration(StringClassMatcher)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class StringFindStartswithCheck : public ClangTidyCheck {
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;

private:
const std::vector<std::string> StringLikeClasses;
const std::vector<StringRef> StringLikeClasses;
utils::IncludeInserter IncludeInserter;
const std::string AbseilStringsMatchHeader;
const StringRef AbseilStringsMatchHeader;
};

} // namespace abseil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ static const char DefaultStringLikeClasses[] = "::std::basic_string;"
static const char DefaultAbseilStringsMatchHeader[] = "absl/strings/match.h";

static transformer::RewriteRuleWith<std::string>
makeRewriteRule(const std::vector<std::string> &StringLikeClassNames,
makeRewriteRule(ArrayRef<StringRef> StringLikeClassNames,
StringRef AbseilStringsMatchHeader) {
auto StringLikeClass = cxxRecordDecl(hasAnyName(SmallVector<StringRef, 4>(
StringLikeClassNames.begin(), StringLikeClassNames.end())));
auto StringLikeClass = cxxRecordDecl(hasAnyName(StringLikeClassNames));
auto StringType =
hasUnqualifiedDesugaredType(recordType(hasDeclaration(StringLikeClass)));
auto CharStarType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class StringFindStrContainsCheck : public utils::TransformerClangTidyCheck {
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;

private:
const std::vector<std::string> StringLikeClassesOption;
const std::string AbseilStringsMatchHeaderOption;
const std::vector<StringRef> StringLikeClassesOption;
const StringRef AbseilStringsMatchHeaderOption;
};

} // namespace abseil
Expand Down
5 changes: 3 additions & 2 deletions clang-tools-extra/clang-tidy/add_new_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,9 @@ def add_release_notes(module_path, module, check_name):
# Adds a test for the check.
def write_test(module_path, module, check_name, test_extension):
check_name_dashes = module + '-' + check_name
filename = os.path.normpath(os.path.join(module_path, '../../test/clang-tidy/checkers',
check_name_dashes + '.' + test_extension))
filename = os.path.normpath(os.path.join(
module_path, '..', '..', 'test', 'clang-tidy', 'checkers',
module, check_name + '.' + test_extension))
print('Creating %s...' % filename)
with io.open(filename, 'w', encoding='utf8', newline='\n') as f:
f.write("""// RUN: %%check_clang_tidy %%s %(check_name_dashes)s %%t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ComparisonInTempFailureRetryCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;

private:
const std::string RawRetryList;
const StringRef RawRetryList;
SmallVector<StringRef, 5> RetryMacros;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ AssertSideEffectCheck::AssertSideEffectCheck(StringRef Name,
: ClangTidyCheck(Name, Context),
CheckFunctionCalls(Options.get("CheckFunctionCalls", false)),
RawAssertList(Options.get("AssertMacros", "assert,NSAssert,NSCAssert")),
IgnoredFunctions(utils::options::parseStringList(
"__builtin_expect;" + Options.get("IgnoredFunctions", ""))) {
IgnoredFunctions(utils::options::parseListPair(
"__builtin_expect;", Options.get("IgnoredFunctions", ""))) {
StringRef(RawAssertList).split(AssertMacros, ",", -1, false);
}

Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class AssertSideEffectCheck : public ClangTidyCheck {

private:
const bool CheckFunctionCalls;
const std::string RawAssertList;
const StringRef RawAssertList;
SmallVector<StringRef, 5> AssertMacros;
const std::vector<std::string> IgnoredFunctions;
const std::vector<StringRef> IgnoredFunctions;
};

} // namespace bugprone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ DanglingHandleCheck::DanglingHandleCheck(StringRef Name,
HandleClasses(utils::options::parseStringList(Options.get(
"HandleClasses",
"std::basic_string_view;std::experimental::basic_string_view"))),
IsAHandle(cxxRecordDecl(hasAnyName(std::vector<StringRef>(
HandleClasses.begin(), HandleClasses.end())))
.bind("handle")) {}
IsAHandle(cxxRecordDecl(hasAnyName(HandleClasses)).bind("handle")) {}

void DanglingHandleCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "HandleClasses",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DanglingHandleCheck : public ClangTidyCheck {
void registerMatchersForVariables(ast_matchers::MatchFinder *Finder);
void registerMatchersForReturn(ast_matchers::MatchFinder *Finder);

const std::vector<std::string> HandleClasses;
const std::vector<StringRef> HandleClasses;
const ast_matchers::internal::Matcher<RecordDecl> IsAHandle;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DynamicStaticInitializersCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;

private:
const std::string RawStringHeaderFileExtensions;
const StringRef RawStringHeaderFileExtensions;
utils::FileExtensionsSet HeaderFileExtensions;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ static bool isIgnoredParameter(const TheCheck &Check, const ParmVarDecl *Node) {
LLVM_DEBUG(llvm::dbgs() << "\tType name is '" << NodeTypeName << "'\n");
if (!NodeTypeName.empty()) {
if (llvm::any_of(Check.IgnoredParameterTypeSuffixes,
[NodeTypeName](const std::string &E) {
[NodeTypeName](StringRef E) {
return !E.empty() && NodeTypeName.endswith(E);
})) {
LLVM_DEBUG(llvm::dbgs() << "\tType suffix ignored.\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class EasilySwappableParametersCheck : public ClangTidyCheck {
const std::size_t MinimumLength;

/// The parameter names (as written in the source text) to be ignored.
const std::vector<std::string> IgnoredParameterNames;
const std::vector<StringRef> IgnoredParameterNames;

/// The parameter typename suffixes (as written in the source code) to be
/// ignored.
const std::vector<std::string> IgnoredParameterTypeSuffixes;
const std::vector<StringRef> IgnoredParameterTypeSuffixes;

/// Whether to consider differently qualified versions of the same type
/// mixable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static std::string getNonReservedFixup(std::string Name) {
static Optional<RenamerClangTidyCheck::FailureInfo>
getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace,
const LangOptions &LangOpts, bool Invert,
ArrayRef<std::string> AllowedIdentifiers) {
ArrayRef<StringRef> AllowedIdentifiers) {
assert(!Name.empty());
if (llvm::is_contained(AllowedIdentifiers, Name))
return None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace bugprone {
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/reserved-identifier.html
class ReservedIdentifierCheck final : public RenamerClangTidyCheck {
const bool Invert;
const std::vector<std::string> AllowedIdentifiers;
const std::vector<StringRef> AllowedIdentifiers;

public:
ReservedIdentifierCheck(StringRef Name, ClangTidyContext *Context);
Expand Down
10 changes: 2 additions & 8 deletions clang-tools-extra/clang-tidy/bugprone/SignedCharMisuseCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ namespace bugprone {

static constexpr int UnsignedASCIIUpperBound = 127;

static Matcher<TypedefDecl> hasAnyListedName(const std::string &Names) {
const std::vector<std::string> NameList =
utils::options::parseStringList(Names);
return hasAnyName(std::vector<StringRef>(NameList.begin(), NameList.end()));
}

SignedCharMisuseCheck::SignedCharMisuseCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
Expand All @@ -46,8 +40,8 @@ BindableMatcher<clang::Stmt> SignedCharMisuseCheck::charCastExpression(
// We can ignore typedefs which are some kind of integer types
// (e.g. typedef char sal_Int8). In this case, we don't need to
// worry about the misinterpretation of char values.
const auto IntTypedef = qualType(
hasDeclaration(typedefDecl(hasAnyListedName(CharTypdefsToIgnoreList))));
const auto IntTypedef = qualType(hasDeclaration(typedefDecl(
hasAnyName(utils::options::parseStringList(CharTypdefsToIgnoreList)))));

auto CharTypeExpr = expr();
if (IsSigned) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SignedCharMisuseCheck : public ClangTidyCheck {
const ast_matchers::internal::Matcher<clang::QualType> &IntegerType,
const std::string &CastBindName) const;

const std::string CharTypdefsToIgnoreList;
const StringRef CharTypdefsToIgnoreList;
const bool DiagnoseSignedUnsignedCharComparisons;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const char DefaultStringNames[] =
"::std::basic_string;::std::basic_string_view";

static std::vector<StringRef>
removeNamespaces(const std::vector<std::string> &Names) {
removeNamespaces(const std::vector<StringRef> &Names) {
std::vector<StringRef> Result;
Result.reserve(Names.size());
for (StringRef Name : Names) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StringConstructorCheck : public ClangTidyCheck {
const bool IsStringviewNullptrCheckEnabled;
const bool WarnOnLargeLength;
const unsigned int LargeLengthThreshold;
std::vector<std::string> StringNames;
std::vector<StringRef> StringNames;
};

} // namespace bugprone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class SuspiciousIncludeCheck : public ClangTidyCheck {
utils::FileExtensionsSet ImplementationFileExtensions;

private:
const std::string RawStringHeaderFileExtensions;
const std::string RawStringImplementationFileExtensions;
const StringRef RawStringHeaderFileExtensions;
const StringRef RawStringImplementationFileExtensions;
};

} // namespace bugprone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ SuspiciousMissingCommaCheck::SuspiciousMissingCommaCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
SizeThreshold(Options.get("SizeThreshold", 5U)),
RatioThreshold(std::stod(Options.get("RatioThreshold", ".2"))),
RatioThreshold(std::stod(Options.get("RatioThreshold", ".2").str())),
MaxConcatenatedTokens(Options.get("MaxConcatenatedTokens", 5U)) {}

void SuspiciousMissingCommaCheck::storeOptions(
Expand Down
Loading