[clang] [Coverage][Expansion] handle nested macros in scratch space (PR #89869)

NAKAMURA Takumi via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 24 14:21:56 PDT 2024


================
@@ -292,10 +292,22 @@ class CoverageMappingBuilder {
     return SM.getLocForEndOfFile(SM.getFileID(Loc));
   }
 
-  /// Find out where the current file is included or macro is expanded.
-  SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
-    return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).getBegin()
-                           : SM.getIncludeLoc(SM.getFileID(Loc));
+  /// Find out where the current file is included or macro is expanded. If
+  /// \c AcceptScratch is set to false, keep looking for expansions until the
+  /// found sloc is not a <scratch space>
+  SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc,
+                                          bool AcceptScratch = true) {
+    if (Loc.isMacroID()) {
+      Loc = SM.getImmediateExpansionRange(Loc).getBegin();
+      if (!AcceptScratch)
+        while (Loc.isMacroID() &&
+               SM.isWrittenInScratchSpace(SM.getSpellingLoc(Loc))) {
+          auto ExpansionRange = SM.getImmediateExpansionRange(Loc);
+          Loc = ExpansionRange.getBegin();
+        }
+    } else
+      Loc = SM.getIncludeLoc(SM.getFileID(Loc));
----------------
chapuni wrote:

We prefer early return.

```
if (!Loc.isMacroID())
  return SM.getIncludeLoc(...);
Loc = ...;
if (AcceptScratch)
  return Loc;
while (...)
  ...
```
```

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


More information about the cfe-commits mailing list