[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 22:02:38 PDT 2024


================
@@ -292,10 +292,36 @@ 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 a macro is expanded. If the immediate result is a
+  /// <scratch space>, keep looking until the result isn't. Return the source
+  /// range of the found result, or std::nullopt if the while loop didn't get
+  /// executed, which means the location wasn't changed.
+  std::optional<SourceRange> getNonScratchExpansion(SourceLocation Loc) {
+    std::optional<SourceLocation> EndLoc = std::nullopt;
+    while (Loc.isMacroID() &&
+           SM.isWrittenInScratchSpace(SM.getSpellingLoc(Loc))) {
+      auto ExpansionRange = SM.getImmediateExpansionRange(Loc);
+      Loc = ExpansionRange.getBegin();
+      EndLoc = ExpansionRange.getEnd();
+    }
+    if (EndLoc.has_value())
+      return SourceRange(Loc, EndLoc.value());
----------------
chapuni wrote:

I wrongly supposed `isWrittenInScratchSpace()` would return `SourceRange`. Actually returns `CharSourceRange`.

You may rewind them with your previous change, if you prefer. The current implementation is not clean a bit.

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


More information about the cfe-commits mailing list