-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[analyzer] Accept C library functions from the std
namespace
#84469
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
NagyDonat
merged 5 commits into
llvm:main
from
Ericsson:allow_clibrary_from_namespace_std
Mar 12, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c357aa9
[analyzer] Accept C library functions from the `std` namespace
NagyDonat 6afadd8
Don't match methods from the namespace `std`
NagyDonat 2cc0c75
Tweak comments, fix word order
NagyDonat a15d6bc
Add unittests
steakhal 4e91ad4
Explain why static global functions are rejected
NagyDonat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,9 +87,11 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD, | |
if (!II) | ||
return false; | ||
|
||
// Look through 'extern "C"' and anything similar invented in the future. | ||
// If this function is not in TU directly, it is not a C library function. | ||
if (!FD->getDeclContext()->getRedeclContext()->isTranslationUnit()) | ||
// C library functions are either declared directly within a TU (the common | ||
// case) or they are accessed through the namespace `std` (when they are used | ||
// in C++ via headers like <cstdlib>). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
const DeclContext *DC = FD->getDeclContext()->getRedeclContext(); | ||
if (!(DC->isTranslationUnit() || DC->isStdNamespace())) | ||
return false; | ||
|
||
// If this function is not externally visible, it is not a C library function. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
#include "clang/ASTMatchers/ASTMatchers.h" | ||
#include "clang/Analysis/AnalysisDeclContext.h" | ||
#include "clang/Frontend/ASTUnit.h" | ||
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" | ||
#include "clang/Tooling/Tooling.h" | ||
#include "gtest/gtest.h" | ||
|
||
#include <memory> | ||
|
||
using namespace clang; | ||
using namespace ento; | ||
using namespace ast_matchers; | ||
|
||
testing::AssertionResult extractFunctionDecl(StringRef Code, | ||
const FunctionDecl *&Result) { | ||
auto ASTUnit = tooling::buildASTFromCode(Code); | ||
if (!ASTUnit) | ||
return testing::AssertionFailure() << "AST construction failed"; | ||
|
||
ASTContext &Context = ASTUnit->getASTContext(); | ||
if (Context.getDiagnostics().hasErrorOccurred()) | ||
return testing::AssertionFailure() << "Compilation error"; | ||
|
||
auto Matches = ast_matchers::match(functionDecl().bind("fn"), Context); | ||
if (Matches.empty()) | ||
return testing::AssertionFailure() << "No function declaration found"; | ||
|
||
if (Matches.size() > 1) | ||
return testing::AssertionFailure() | ||
<< "Multiple function declarations found"; | ||
|
||
Result = Matches[0].getNodeAs<FunctionDecl>("fn"); | ||
return testing::AssertionSuccess(); | ||
} | ||
|
||
TEST(IsCLibraryFunctionTest, AcceptsGlobal) { | ||
const FunctionDecl *Result; | ||
ASSERT_TRUE(extractFunctionDecl(R"cpp(void fun();)cpp", Result)); | ||
EXPECT_TRUE(CheckerContext::isCLibraryFunction(Result)); | ||
} | ||
|
||
TEST(IsCLibraryFunctionTest, AcceptsExternCGlobal) { | ||
const FunctionDecl *Result; | ||
ASSERT_TRUE( | ||
extractFunctionDecl(R"cpp(extern "C" { void fun(); })cpp", Result)); | ||
EXPECT_TRUE(CheckerContext::isCLibraryFunction(Result)); | ||
} | ||
|
||
TEST(IsCLibraryFunctionTest, RejectsNoInlineNoExternalLinkage) { | ||
// Functions that are neither inlined nor externally visible cannot be C library functions. | ||
const FunctionDecl *Result; | ||
ASSERT_TRUE(extractFunctionDecl(R"cpp(static void fun();)cpp", Result)); | ||
EXPECT_FALSE(CheckerContext::isCLibraryFunction(Result)); | ||
} | ||
|
||
TEST(IsCLibraryFunctionTest, RejectsAnonymousNamespace) { | ||
const FunctionDecl *Result; | ||
ASSERT_TRUE( | ||
extractFunctionDecl(R"cpp(namespace { void fun(); })cpp", Result)); | ||
EXPECT_FALSE(CheckerContext::isCLibraryFunction(Result)); | ||
} | ||
|
||
TEST(IsCLibraryFunctionTest, AcceptsStdNamespace) { | ||
const FunctionDecl *Result; | ||
ASSERT_TRUE( | ||
extractFunctionDecl(R"cpp(namespace std { void fun(); })cpp", Result)); | ||
EXPECT_TRUE(CheckerContext::isCLibraryFunction(Result)); | ||
} | ||
|
||
TEST(IsCLibraryFunctionTest, RejectsOtherNamespaces) { | ||
const FunctionDecl *Result; | ||
ASSERT_TRUE( | ||
extractFunctionDecl(R"cpp(namespace stdx { void fun(); })cpp", Result)); | ||
EXPECT_FALSE(CheckerContext::isCLibraryFunction(Result)); | ||
} | ||
|
||
TEST(IsCLibraryFunctionTest, RejectsClassStatic) { | ||
const FunctionDecl *Result; | ||
ASSERT_TRUE( | ||
extractFunctionDecl(R"cpp(class A { static void fun(); };)cpp", Result)); | ||
EXPECT_FALSE(CheckerContext::isCLibraryFunction(Result)); | ||
} | ||
|
||
TEST(IsCLibraryFunctionTest, RejectsClassMember) { | ||
const FunctionDecl *Result; | ||
ASSERT_TRUE(extractFunctionDecl(R"cpp(class A { void fun(); };)cpp", Result)); | ||
EXPECT_FALSE(CheckerContext::isCLibraryFunction(Result)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might as well go the extra mile and mention an example, like
std::malloc
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think saying "functions from the C standard library" (at the beginning of this comment block) is clear enough, mentioning
malloc
is redundant after it. Also, technically, we are not using this mode formalloc
yet.