Skip to content
Open
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
14 changes: 10 additions & 4 deletions clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,25 @@ std::optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP) {
if (InvalidSpelling)
return std::nullopt;

llvm::APInt IntValue;
llvm::APSInt IntValue(0, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
llvm::APSInt IntValue(0, true);
llvm::APSInt IntValue(/*BitWidth=*/0, /*isUnsigned=*/true);

constexpr unsigned AutoSenseRadix = 0;
if (ValueStr.getAsInteger(AutoSenseRadix, IntValue))
if (ValueStr.getAsInteger(AutoSenseRadix,
static_cast<llvm::APInt &>(IntValue)))
Comment on lines +147 to +148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this static cast? It doesn't seem to be the case. Implicit conversion should just achieve this, no?

return std::nullopt;

// Parse an optional minus sign.
size_t Size = FilteredTokens.size();
if (Size >= 2) {
if (FilteredTokens[Size - 2].is(tok::minus))
if (FilteredTokens[Size - 2].is(tok::minus)) {
// Make sure there's space for a sign bit
if (IntValue.isSignBitSet())
IntValue = IntValue.extend(IntValue.getBitWidth() + 1);
IntValue.setIsUnsigned(false);
IntValue = -IntValue;
}
}

return IntValue.getSExtValue();
return IntValue.getExtValue();
}

OperatorKind operationKindFromOverloadedOperator(OverloadedOperatorKind OOK,
Expand Down
16 changes: 16 additions & 0 deletions clang/test/Analysis/std-c-library-functions-eof-2-rad.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %clang_analyze_cc1 -std=c23 -analyzer-checker=core,unix.StdCLibraryFunctions,debug.ExprInspection -verify -analyzer-config eagerly-assume=false %s

void clang_analyzer_eval(int);

typedef struct FILE FILE;
/// Test that the static analyzer doesn't interpret the most significant bit as the sign bit.
// Unorthodox EOF value with a power of 2 radix
#define EOF (-0b11)

int getc(FILE *);
void test_getc(FILE *fp) {
int y = getc(fp);
if (y < 0) {
clang_analyzer_eval(y == EOF); // expected-warning{{TRUE}}
}
}