Skip to content

[StaticAnalyzer] Fix tryExpandAsInteger's failures on PCH macros #142722

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 3 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,19 @@ std::optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP) {

// Parse an integer at the end of the macro definition.
const Token &T = FilteredTokens.back();
// FIXME: EOF macro token coming from a PCH file on macOS while marked as
// literal, doesn't contain any literal data
if (!T.isLiteral() || !T.getLiteralData())

if (!T.isLiteral())
return std::nullopt;
StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());

bool InvalidSpelling = false;
SmallVector<char> Buffer(T.getLength());
// `Preprocessor::getSpelling` can get the spelling of the token regardless of
// whether the macro is defined in a PCH or not:
StringRef ValueStr = PP.getSpelling(T, Buffer, &InvalidSpelling);

if (InvalidSpelling)
return std::nullopt;

llvm::APInt IntValue;
constexpr unsigned AutoSenseRadix = 0;
if (ValueStr.getAsInteger(AutoSenseRadix, IntValue))
Expand Down
28 changes: 0 additions & 28 deletions clang/test/Analysis/pch_crash.cpp

This file was deleted.

50 changes: 50 additions & 0 deletions clang/test/Analysis/pch_macro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t

// DEFINE: %{emit-pch-action} = \
// DEFINE: %clang_cc1 -x c++ -emit-pch -o %t/header.pch %t/header.h

// DEFINE: %{analyze-action} = \
// DEFINE: %clang_analyze_cc1 -include-pch %t/header.pch \
// DEFINE: -analyzer-checker=core,apiModeling,unix.StdCLibraryFunctions \
// DEFINE: -verify %t/main.cpp

// RUN: %{emit-pch-action} -triple x86_64-apple-macosx10.15.0
// RUN: %{analyze-action} -triple x86_64-apple-macosx10.15.0
// RUN: %{emit-pch-action}
// RUN: %{analyze-action}


//--- header.h


// Pre-compiled header

int foo();

// Literal data for macro values will be null as they are defined in a PCH
#define EOF -1
#define AT_FDCWD -2


//--- main.cpp


// Source file
// expected-no-diagnostics
int test() {
// we need a function call here to initiate erroneous routine
return foo(); // no-crash
}

// Test that StdLibraryFunctionsChecker can obtain the definition of
// AT_FDCWD even if it is from a PCH:
int faccessat(int, const char *, int, int);

void test_faccessat() {
char fileSystemPath[10] = { 0 };

if (0 != faccessat(AT_FDCWD, fileSystemPath, 2, 0x0030)) {}
}

Loading