[clang] [StaticAnalyzer] Fix tryExpandAsInteger's failures on PCH macros (PR #142722)
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 5 00:50:18 PDT 2025
================
@@ -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;
+ // `Preprocessor::getSpelling` can get the spelling of the token regardless of
+ // whether the macro is defined in a PCH or not:
+ std::string Spelling = PP.getSpelling(T, &InvalidSpelling);
----------------
steakhal wrote:
You were right, it had to allocate because it needs to do some sort of "cleaning".
Despite I suggested this, the potential allocation is unavoidable because it needs to own the buffer.
I didn't know about this, so feel free to revert this part back because that is actually shorter than passing a buffer directly.
Anyway, I don't mind either of these. There is every day I learn something.
https://github.com/llvm/llvm-project/pull/142722
More information about the cfe-commits
mailing list