[clang] [StaticAnalyzer] Fix tryExpandAsInteger's failures on PCH macros (PR #142722)

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 3 23:13:55 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:

`getSpelling` appears to return StringRef. We could bind this directly to `ValueStr` and avoid a possible allocation I think.

https://github.com/llvm/llvm-project/pull/142722


More information about the cfe-commits mailing list