[clang] [Coverage][Expansion] handle nested macros in scratch space (PR #89869)
Wentao Zhang via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 24 23:05:02 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());
----------------
whentojump wrote:
> Actually returns CharSourceRange.
True. Ideally we can circulate around `CharSourceRange` without converting back and forth too much but I found it hard to make both two callers clean and straightforward.
> You may rewind them
I can do that, meanwhile I'll also seek if there's a better way
https://github.com/llvm/llvm-project/pull/89869
More information about the cfe-commits
mailing list