Skip to content

Commit 6ac4585

Browse files
[clang][AST] Do not try to handle irrelevant cases in writeBareSourceLocation (#166588)
`writeBareSourceLocation` is always called on either `Expanded` or `Spelling` location, in any on those cases the `SM.getSpellingLineNumber(Loc) == SM.getExpansionLineNumber(Loc) == SM.getLineNumber(Loc)`.
1 parent ba4abc6 commit 6ac4585

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

clang/include/clang/AST/JSONNodeDumper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class JSONNodeDumper
149149
void writeIncludeStack(PresumedLoc Loc, bool JustFirst = false);
150150

151151
// Writes the attributes of a SourceLocation object without.
152-
void writeBareSourceLocation(SourceLocation Loc, bool IsSpelling);
152+
void writeBareSourceLocation(SourceLocation Loc);
153153

154154
// Writes the attributes of a SourceLocation to JSON based on its presumed
155155
// spelling location. If the given location represents a macro invocation,

clang/lib/AST/JSONNodeDumper.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,13 @@ void JSONNodeDumper::writeIncludeStack(PresumedLoc Loc, bool JustFirst) {
272272
JOS.attributeEnd();
273273
}
274274

275-
void JSONNodeDumper::writeBareSourceLocation(SourceLocation Loc,
276-
bool IsSpelling) {
275+
void JSONNodeDumper::writeBareSourceLocation(SourceLocation Loc) {
277276
PresumedLoc Presumed = SM.getPresumedLoc(Loc);
278-
unsigned ActualLine = IsSpelling ? SM.getSpellingLineNumber(Loc)
279-
: SM.getExpansionLineNumber(Loc);
280-
StringRef ActualFile = SM.getBufferName(Loc);
281-
282277
if (Presumed.isValid()) {
283-
JOS.attribute("offset", SM.getDecomposedLoc(Loc).second);
278+
StringRef ActualFile = SM.getBufferName(Loc);
279+
auto [FID, FilePos] = SM.getDecomposedLoc(Loc);
280+
unsigned ActualLine = SM.getLineNumber(FID, FilePos);
281+
JOS.attribute("offset", FilePos);
284282
if (LastLocFilename != ActualFile) {
285283
JOS.attribute("file", ActualFile);
286284
JOS.attribute("line", ActualLine);
@@ -318,18 +316,17 @@ void JSONNodeDumper::writeSourceLocation(SourceLocation Loc) {
318316
if (Expansion != Spelling) {
319317
// If the expansion and the spelling are different, output subobjects
320318
// describing both locations.
321-
JOS.attributeObject("spellingLoc", [Spelling, this] {
322-
writeBareSourceLocation(Spelling, /*IsSpelling*/ true);
323-
});
319+
JOS.attributeObject(
320+
"spellingLoc", [Spelling, this] { writeBareSourceLocation(Spelling); });
324321
JOS.attributeObject("expansionLoc", [Expansion, Loc, this] {
325-
writeBareSourceLocation(Expansion, /*IsSpelling*/ false);
322+
writeBareSourceLocation(Expansion);
326323
// If there is a macro expansion, add extra information if the interesting
327324
// bit is the macro arg expansion.
328325
if (SM.isMacroArgExpansion(Loc))
329326
JOS.attribute("isMacroArgExpansion", true);
330327
});
331328
} else
332-
writeBareSourceLocation(Spelling, /*IsSpelling*/ true);
329+
writeBareSourceLocation(Spelling);
333330
}
334331

335332
void JSONNodeDumper::writeSourceRange(SourceRange R) {

0 commit comments

Comments
 (0)