================
@@ -230,6 +241,37 @@ FormatStringConverter::FormatStringConverter(ASTContext 
*ContextIn,
   finalizeFormatText();
 }
 
+std::optional<StringRef>
+FormatStringConverter::formatStringContainsUnreplaceableMacro(
+    const StringLiteral *FormatExpr, SourceManager &SM, Preprocessor &PP) {
+  for (auto I = FormatExpr->tokloc_begin(), E = FormatExpr->tokloc_end();
+       I != E; ++I) {
+    const SourceLocation &TokenLoc = *I;
+    if (TokenLoc.isMacroID()) {
+      const StringRef MacroName =
+          Lexer::getImmediateMacroName(TokenLoc, SM, PP.getLangOpts());
+
+      // glibc uses __PRI64_PREFIX and __PRIPTR_PREFIX to define the prefixes
+      // for types that change size so we must look for multiple prefixes.
+      if (!MacroName.starts_with("PRI") && !MacroName.starts_with("__PRI"))
+        return MacroName;
+
+      const SourceLocation TokenSpellingLoc = SM.getSpellingLoc(TokenLoc);
----------------
mikecrowe wrote:

Do you mean that I should check whether the `SourceLocation` returned from 
`SourceManager::getSpellingLoc()` is valid?

Looking at the `SourceLocation` documentation and other checks that call 
`SourceManager::getSpellingLoc()` I was unable to spot how to tell whether the 
location returned is valid. All the other callers I looked at just went on to 
use the returned location without any checking.

Can I arrange for an invalid location to be returned from the lit test somehow 
to test this case?

https://github.com/llvm/llvm-project/pull/97911
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to