llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Rohan Jacob-Rao (rohanjr) <details> <summary>Changes</summary> The struct has no members and has a surrounding namespace that can be used for disambiguating names. This was also mentioned in a previous PR review: https://github.com/llvm/llvm-project/pull/101583/changes#r1737089937 --- Full diff: https://github.com/llvm/llvm-project/pull/178801.diff 1 Files Affected: - (modified) clang/lib/Analysis/UnsafeBufferUsage.cpp (+35-43) ``````````diff diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp index 761aedda7eacf..75ce911d3e8dc 100644 --- a/clang/lib/Analysis/UnsafeBufferUsage.cpp +++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp @@ -840,14 +840,13 @@ static const Expr *getSubExprInSizeOfExpr(const Expr *E) { return SizeOfExpr->getArgumentExpr()->IgnoreParenImpCasts(); } -namespace libc_func_matchers { // Under `libc_func_matchers`, define a set of matchers that match unsafe // functions in libc and unsafe calls to them. - +namespace libc_func_matchers { // A tiny parser to strip off common prefix and suffix of libc function names // in real code. // -// Given a function name, `matchName` returns `CoreName` according to the +// Given a function name, `matchName()` returns `CoreName` according to the // following grammar: // // LibcName := CoreName | CoreName + "_s" @@ -855,35 +854,33 @@ namespace libc_func_matchers { // "__builtin___" + LibcName + "_chk" | // "__asan_" + LibcName // -struct LibcFunNamePrefixSuffixParser { - StringRef matchName(StringRef FunName, bool isBuiltin) { - // Try to match __builtin_: - if (isBuiltin && FunName.starts_with("__builtin_")) - // Then either it is __builtin_LibcName or __builtin___LibcName_chk or - // no match: - return matchLibcNameOrBuiltinChk( - FunName.drop_front(10 /* truncate "__builtin_" */)); - // Try to match __asan_: - if (FunName.starts_with("__asan_")) - return matchLibcName(FunName.drop_front(7 /* truncate of "__asan_" */)); - return matchLibcName(FunName); - } - - // Parameter `Name` is the substring after stripping off the prefix - // "__builtin_". - StringRef matchLibcNameOrBuiltinChk(StringRef Name) { - if (Name.starts_with("__") && Name.ends_with("_chk")) - return matchLibcName( - Name.drop_front(2).drop_back(4) /* truncate "__" and "_chk" */); - return matchLibcName(Name); - } - - StringRef matchLibcName(StringRef Name) { - if (Name.ends_with("_s")) - return Name.drop_back(2 /* truncate "_s" */); - return Name; - } -}; +StringRef matchLibcName(StringRef Name) { + if (Name.ends_with("_s")) + return Name.drop_back(2 /* truncate "_s" */); + return Name; +} + +// Parameter `Name` is the substring after stripping off the prefix +// "__builtin_". +StringRef matchLibcNameOrBuiltinChk(StringRef Name) { + if (Name.starts_with("__") && Name.ends_with("_chk")) + return matchLibcName( + Name.drop_front(2).drop_back(4) /* truncate "__" and "_chk" */); + return matchLibcName(Name); +} + +StringRef matchName(StringRef FunName, bool isBuiltin) { + // Try to match __builtin_: + if (isBuiltin && FunName.starts_with("__builtin_")) + // Then either it is __builtin_LibcName or __builtin___LibcName_chk or no + // match: + return matchLibcNameOrBuiltinChk( + FunName.drop_front(10 /* truncate "__builtin_" */)); + // Try to match __asan_: + if (FunName.starts_with("__asan_")) + return matchLibcName(FunName.drop_front(7 /* truncate of "__asan_" */)); + return matchLibcName(FunName); +} // Return true iff at least one of following cases holds: // 1. Format string is a literal and there is an unsafe pointer argument @@ -1038,7 +1035,7 @@ hasUnsafeFormatOrSArg(ASTContext &Ctx, const CallExpr *Call, // 2. `CoreName` or `CoreName[str/wcs]` is one of the `PredefinedNames`, which // is a set of libc function names. // -// Note: For predefined prefix and suffix, see `LibcFunNamePrefixSuffixParser`. +// Note: For predefined prefix and suffix, see `matchName()`. // The notation `CoreName[str/wcs]` means a new name obtained from replace // string "wcs" with "str" in `CoreName`. static bool isPredefinedUnsafeLibcFunc(const FunctionDecl &Node) { @@ -1121,8 +1118,7 @@ static bool isPredefinedUnsafeLibcFunc(const FunctionDecl &Node) { if (!II) return false; - StringRef Name = LibcFunNamePrefixSuffixParser().matchName( - II->getName(), Node.getBuiltinID()); + StringRef Name = matchName(II->getName(), Node.getBuiltinID()); // Match predefined names: if (PredefinedNames->find(Name) != PredefinedNames->end()) @@ -1155,8 +1151,7 @@ static bool isUnsafeMemset(const CallExpr &Node, ASTContext &Ctx) { if (!II) return false; - StringRef Name = LibcFunNamePrefixSuffixParser().matchName( - II->getName(), FD->getBuiltinID()); + StringRef Name = matchName(II->getName(), FD->getBuiltinID()); if (Name != "memset") return false; @@ -1191,8 +1186,7 @@ static bool isUnsafeVaListPrintfFunc(const FunctionDecl &Node) { if (!II) return false; - StringRef Name = LibcFunNamePrefixSuffixParser().matchName( - II->getName(), Node.getBuiltinID()); + StringRef Name = matchName(II->getName(), Node.getBuiltinID()); if (!Name.ends_with("printf")) return false; // neither printf nor scanf @@ -1207,8 +1201,7 @@ static bool isUnsafeSprintfFunc(const FunctionDecl &Node) { if (!II) return false; - StringRef Name = LibcFunNamePrefixSuffixParser().matchName( - II->getName(), Node.getBuiltinID()); + StringRef Name = matchName(II->getName(), Node.getBuiltinID()); if (!Name.ends_with("printf") || // Let `isUnsafeVaListPrintfFunc` check for cases with va-list: @@ -1231,8 +1224,7 @@ static bool isNormalPrintfFunc(const FunctionDecl &Node) { if (!II) return false; - StringRef Name = LibcFunNamePrefixSuffixParser().matchName( - II->getName(), Node.getBuiltinID()); + StringRef Name = matchName(II->getName(), Node.getBuiltinID()); if (!Name.ends_with("printf") || Name.starts_with("v")) return false; `````````` </details> https://github.com/llvm/llvm-project/pull/178801 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
