[clang-tools-extra] [clang-tidy] Add user-defined functions to `bugprone-unsafe-functions` check (PR #106350)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 28 01:48:36 PDT 2024
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 5f15c1776a462940464743dbc9e82c46fe7e14aa 80694ad203d0d37cd8d186d823bb536c921ae9bf --extensions c,cpp,h -- clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions-custom-regex.cpp clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions-custom.c clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.h clang-tools-extra/clang-tidy/utils/Matchers.h clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp
index 8d9ffda04a..c2f23c309f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp
@@ -34,8 +34,10 @@ static constexpr llvm::StringLiteral FunctionNamesWithAnnexKReplacementId =
static constexpr llvm::StringLiteral FunctionNamesId = "FunctionsNames";
static constexpr llvm::StringLiteral AdditionalFunctionNamesId =
"AdditionalFunctionsNames";
-static constexpr llvm::StringLiteral CustomFunctionNamesId = "CustomFunctionNames";
-static constexpr llvm::StringLiteral CustomAnnexKFunctionNamesId = "CustomAnnexKFunctionNames";
+static constexpr llvm::StringLiteral CustomFunctionNamesId =
+ "CustomFunctionNames";
+static constexpr llvm::StringLiteral CustomAnnexKFunctionNamesId =
+ "CustomAnnexKFunctionNames";
static constexpr llvm::StringLiteral DeclRefId = "DRE";
static std::optional<std::string>
@@ -138,7 +140,7 @@ static bool isAnnexKAvailable(std::optional<bool> &CacheVar, Preprocessor *PP,
}
static std::vector<UnsafeFunctionsCheck::CheckedFunction>
-ParseCheckedFunctions(StringRef Option, StringRef OptionName,
+ParseCheckedFunctions(StringRef Option, StringRef OptionName,
ClangTidyContext *Context) {
std::vector<StringRef> Functions = utils::options::parseStringList(Option);
std::vector<UnsafeFunctionsCheck::CheckedFunction> Result;
@@ -153,19 +155,22 @@ ParseCheckedFunctions(StringRef Option, StringRef OptionName,
auto [Replacement, Reason] = Rest.split(',');
if (Name.trim().empty()) {
- Context->configurationDiag(
- "invalid configuration value for option '%0'; "
- "expected the name of an unsafe function") << OptionName;
+ Context->configurationDiag("invalid configuration value for option '%0'; "
+ "expected the name of an unsafe function")
+ << OptionName;
}
if (Replacement.trim().empty()) {
Context->configurationDiag(
- "invalid configuration value '%0' for option '%1'; "
- "expected a replacement function name") << Name.trim() << OptionName;
+ "invalid configuration value '%0' for option '%1'; "
+ "expected a replacement function name")
+ << Name.trim() << OptionName;
}
- Result.push_back({ Name.trim().str(), matchers::MatchesAnyListedNameMatcher::NameMatcher(Name.trim()),
- Replacement.trim().str(), Reason.trim().str() });
+ Result.push_back(
+ {Name.trim().str(),
+ matchers::MatchesAnyListedNameMatcher::NameMatcher(Name.trim()),
+ Replacement.trim().str(), Reason.trim().str()});
}
return Result;
@@ -196,14 +201,15 @@ UnsafeFunctionsCheck::UnsafeFunctionsCheck(StringRef Name,
CustomAnnexKFunctions(ParseCheckedFunctions(
Options.get(OptionNameCustomAnnexKFunctions, ""),
OptionNameCustomAnnexKFunctions, Context)),
- ReportDefaultFunctions(Options.get(OptionNameReportDefaultFunctions, true)),
+ ReportDefaultFunctions(
+ Options.get(OptionNameReportDefaultFunctions, true)),
ReportMoreUnsafeFunctions(
Options.get(OptionNameReportMoreUnsafeFunctions, true)) {}
void UnsafeFunctionsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, OptionNameCustomNormalFunctions,
+ Options.store(Opts, OptionNameCustomNormalFunctions,
SerializeCheckedFunctions(CustomNormalFunctions));
- Options.store(Opts, OptionNameCustomAnnexKFunctions,
+ Options.store(Opts, OptionNameCustomAnnexKFunctions,
SerializeCheckedFunctions(CustomAnnexKFunctions));
Options.store(Opts, OptionNameReportDefaultFunctions, ReportDefaultFunctions);
Options.store(Opts, OptionNameReportMoreUnsafeFunctions,
@@ -215,33 +221,34 @@ void UnsafeFunctionsCheck::registerMatchers(MatchFinder *Finder) {
if (getLangOpts().C11) {
// Matching functions with safe replacements only in Annex K.
auto FunctionNamesWithAnnexKReplacementMatcher = hasAnyName(
- "::bsearch", "::ctime", "::fopen", "::fprintf", "::freopen", "::fscanf",
- "::fwprintf", "::fwscanf", "::getenv", "::gmtime", "::localtime",
- "::mbsrtowcs", "::mbstowcs", "::memcpy", "::memmove", "::memset",
- "::printf", "::qsort", "::scanf", "::snprintf", "::sprintf", "::sscanf",
- "::strcat", "::strcpy", "::strerror", "::strlen", "::strncat",
- "::strncpy", "::strtok", "::swprintf", "::swscanf", "::vfprintf",
- "::vfscanf", "::vfwprintf", "::vfwscanf", "::vprintf", "::vscanf",
- "::vsnprintf", "::vsprintf", "::vsscanf", "::vswprintf", "::vswscanf",
- "::vwprintf", "::vwscanf", "::wcrtomb", "::wcscat", "::wcscpy",
- "::wcslen", "::wcsncat", "::wcsncpy", "::wcsrtombs", "::wcstok",
- "::wcstombs", "::wctomb", "::wmemcpy", "::wmemmove", "::wprintf",
- "::wscanf");
+ "::bsearch", "::ctime", "::fopen", "::fprintf", "::freopen",
+ "::fscanf", "::fwprintf", "::fwscanf", "::getenv", "::gmtime",
+ "::localtime", "::mbsrtowcs", "::mbstowcs", "::memcpy", "::memmove",
+ "::memset", "::printf", "::qsort", "::scanf", "::snprintf",
+ "::sprintf", "::sscanf", "::strcat", "::strcpy", "::strerror",
+ "::strlen", "::strncat", "::strncpy", "::strtok", "::swprintf",
+ "::swscanf", "::vfprintf", "::vfscanf", "::vfwprintf", "::vfwscanf",
+ "::vprintf", "::vscanf", "::vsnprintf", "::vsprintf", "::vsscanf",
+ "::vswprintf", "::vswscanf", "::vwprintf", "::vwscanf", "::wcrtomb",
+ "::wcscat", "::wcscpy", "::wcslen", "::wcsncat", "::wcsncpy",
+ "::wcsrtombs", "::wcstok", "::wcstombs", "::wctomb", "::wmemcpy",
+ "::wmemmove", "::wprintf", "::wscanf");
Finder->addMatcher(
declRefExpr(to(functionDecl(FunctionNamesWithAnnexKReplacementMatcher)
.bind(FunctionNamesWithAnnexKReplacementId)))
.bind(DeclRefId),
this);
}
-
+
// Matching functions with replacements without Annex K.
auto FunctionNamesMatcher =
hasAnyName("::asctime", "asctime_r", "::gets", "::rewind", "::setbuf");
Finder->addMatcher(
- declRefExpr(to(functionDecl(FunctionNamesMatcher).bind(FunctionNamesId)))
+ declRefExpr(
+ to(functionDecl(FunctionNamesMatcher).bind(FunctionNamesId)))
.bind(DeclRefId),
this);
-
+
if (ReportMoreUnsafeFunctions) {
// Matching functions with replacements without Annex K, at user request.
auto AdditionalFunctionNamesMatcher =
@@ -264,10 +271,10 @@ void UnsafeFunctionsCheck::registerMatchers(MatchFinder *Finder) {
auto CustomAnnexKFunctionsMatcher =
matchers::matchesAnyListedName(FunctionNames);
- Finder->addMatcher(
- declRefExpr(to(functionDecl(CustomAnnexKFunctionsMatcher).bind(CustomAnnexKFunctionNamesId)))
- .bind(DeclRefId),
- this);
+ Finder->addMatcher(declRefExpr(to(functionDecl(CustomAnnexKFunctionsMatcher)
+ .bind(CustomAnnexKFunctionNamesId)))
+ .bind(DeclRefId),
+ this);
}
if (!CustomNormalFunctions.empty()) {
@@ -277,13 +284,12 @@ void UnsafeFunctionsCheck::registerMatchers(MatchFinder *Finder) {
for (const auto &Entry : CustomNormalFunctions)
FunctionNames.push_back(Entry.Name);
- auto CustomFunctionsMatcher =
- matchers::matchesAnyListedName(FunctionNames);
+ auto CustomFunctionsMatcher = matchers::matchesAnyListedName(FunctionNames);
- Finder->addMatcher(
- declRefExpr(to(functionDecl(CustomFunctionsMatcher).bind(CustomFunctionNamesId)))
- .bind(DeclRefId),
- this);
+ Finder->addMatcher(declRefExpr(to(functionDecl(CustomFunctionsMatcher)
+ .bind(CustomFunctionNamesId)))
+ .bind(DeclRefId),
+ this);
}
}
@@ -298,12 +304,12 @@ void UnsafeFunctionsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Normal = Result.Nodes.getNodeAs<FunctionDecl>(FunctionNamesId);
const auto *Additional =
Result.Nodes.getNodeAs<FunctionDecl>(AdditionalFunctionNamesId);
- const auto *CustomAnnexK = Result.Nodes.getNodeAs<FunctionDecl>(
- CustomAnnexKFunctionNamesId);
- const auto *CustomNormal = Result.Nodes.getNodeAs<FunctionDecl>(
- CustomFunctionNamesId);
+ const auto *CustomAnnexK =
+ Result.Nodes.getNodeAs<FunctionDecl>(CustomAnnexKFunctionNamesId);
+ const auto *CustomNormal =
+ Result.Nodes.getNodeAs<FunctionDecl>(CustomFunctionNamesId);
assert((AnnexK || Normal || Additional || CustomAnnexK || CustomNormal) &&
- "No valid match category.");
+ "No valid match category.");
bool AnnexKIsAvailable =
isAnnexKAvailable(IsAnnexKAvailable, PP, getLangOpts());
@@ -311,11 +317,11 @@ void UnsafeFunctionsCheck::check(const MatchFinder::MatchResult &Result) {
if (CustomAnnexK || CustomNormal) {
const auto ShowCheckedFunctionWarning = [&](const CheckedFunction &Entry) {
- StringRef Reason = Entry.Reason.empty() ? "is marked as unsafe"
- : Entry.Reason.c_str();
+ StringRef Reason =
+ Entry.Reason.empty() ? "is marked as unsafe" : Entry.Reason.c_str();
diag(DeclRef->getExprLoc(), "function %0 %1; '%2' should be used instead")
- << FuncDecl << Reason
- << Entry.Replacement << DeclRef->getSourceRange();
+ << FuncDecl << Reason << Entry.Replacement
+ << DeclRef->getSourceRange();
};
if (AnnexKIsAvailable) {
@@ -330,7 +336,7 @@ void UnsafeFunctionsCheck::check(const MatchFinder::MatchResult &Result) {
}
assert(!CustomAnnexK && "No custom Annex K function was matched.");
- }
+ }
// Annex K was not available, or the assertion failed.
if (CustomAnnexK)
diff --git a/clang-tools-extra/clang-tidy/utils/Matchers.h b/clang-tools-extra/clang-tidy/utils/Matchers.h
index f79d872300..451c4ce925 100644
--- a/clang-tools-extra/clang-tidy/utils/Matchers.h
+++ b/clang-tools-extra/clang-tidy/utils/Matchers.h
@@ -137,7 +137,6 @@ public:
}
private:
-
std::vector<NameMatcher> NameMatchers;
};
``````````
</details>
https://github.com/llvm/llvm-project/pull/106350
More information about the cfe-commits
mailing list