Returns the file range for a given Location as a Token Range This is quite similar to getFileLoc in SourceManager as both use getImmediateExpansionRange and getImmediateSpellingLoc (for macro IDs). However: - We want to maintain the full range information as we move from one file to the next. getFileLoc only uses the BeginLoc of getImmediateExpansionRange. - We want to split '>>' tokens as the lex
| 168 | // There is also getExpansionRange but it simply calls |
| 169 | // getImmediateExpansionRange on the begin and ends separately which is wrong. |
| 170 | static SourceRange getTokenFileRange(SourceLocation Loc, |
| 171 | const SourceManager& SM, |
| 172 | const LangOptions& LangOpts) { |
| 173 | SourceRange FileRange = Loc; |
| 174 | while(!FileRange.getBegin().isFileID()) { |
| 175 | if(SM.isMacroArgExpansion(FileRange.getBegin())) { |
| 176 | FileRange = unionTokenRange(SM.getImmediateSpellingLoc(FileRange.getBegin()), |
| 177 | SM.getImmediateSpellingLoc(FileRange.getEnd()), |
| 178 | SM, |
| 179 | LangOpts); |
| 180 | assert(SM.isWrittenInSameFile(FileRange.getBegin(), FileRange.getEnd())); |
| 181 | } else { |
| 182 | SourceRange ExpansionRangeForBegin = |
| 183 | getExpansionTokenRangeInSameFile(FileRange.getBegin(), SM, LangOpts); |
| 184 | SourceRange ExpansionRangeForEnd = |
| 185 | getExpansionTokenRangeInSameFile(FileRange.getEnd(), SM, LangOpts); |
| 186 | if(ExpansionRangeForBegin.isInvalid() || ExpansionRangeForEnd.isInvalid()) |
| 187 | return SourceRange(); |
| 188 | assert(SM.isWrittenInSameFile(ExpansionRangeForBegin.getBegin(), |
| 189 | ExpansionRangeForEnd.getBegin()) && |
| 190 | "Both Expansion ranges should be in same file."); |
| 191 | FileRange = unionTokenRange(ExpansionRangeForBegin, ExpansionRangeForEnd, SM, LangOpts); |
| 192 | } |
| 193 | } |
| 194 | return FileRange; |
| 195 | } |
| 196 | |
| 197 | std::optional<SourceRange> toHalfOpenFileRange(const SourceManager& SM, |
| 198 | const LangOptions& LangOpts, |
no test coverage detected