Skip to content

Commit e8b323f

Browse files
committed
ast-exporter: include full final token in decl source ranges
Clang's SourceRange ends at the beginning of the final token, but this is not useful if we want to look at every character of a declaration, as we may when considering the spelling of the corresponding C definition
1 parent 039dea2 commit e8b323f

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,9 +830,13 @@ class TranslateASTVisitor final
830830
Decl *ast, ASTEntryTag tag, const std::vector<void *> &childIds,
831831
const QualType T,
832832
std::function<void(CborEncoder *)> extra = [](CborEncoder *) {}) {
833+
auto &Mgr = Context->getSourceManager();
833834
auto rvalue = false;
834835
auto encodeMacroExpansions = false;
835-
encode_entry_raw(ast, tag, ast->getSourceRange(), T, rvalue,
836+
auto charRange = clang::CharSourceRange::getCharRange(ast->getSourceRange());
837+
// Extend the range to include the entire final token.
838+
charRange.setEnd(clang::Lexer::getLocForEndOfToken(ast->getSourceRange().getEnd(), 0, Mgr, Context->getLangOpts()));
839+
encode_entry_raw(ast, tag, charRange.getAsRange(), T, rvalue,
836840
isVaList(ast, T), encodeMacroExpansions, childIds, extra);
837841
}
838842

@@ -1998,7 +2002,13 @@ class TranslateASTVisitor final
19982002

19992003
// We prefer non-implicit decls for their type information.
20002004
auto functionType = paramsFD->getType();
2001-
auto span = paramsFD->getSourceRange();
2005+
auto &Mgr = Context->getSourceManager();
2006+
auto rvalue = false;
2007+
auto encodeMacroExpansions = false;
2008+
auto charRange = clang::CharSourceRange::getCharRange(FD->getSourceRange());
2009+
// Extend the range to include the entire final token.
2010+
charRange.setEnd(clang::Lexer::getLocForEndOfToken(FD->getSourceRange().getEnd(), 0, Mgr, Context->getLangOpts()));
2011+
auto span = charRange.getAsRange();
20022012
encode_entry(
20032013
FD, TagFunctionDecl, span, childIds, functionType,
20042014
[this, FD](CborEncoder *array) {
@@ -2119,7 +2129,12 @@ class TranslateASTVisitor final
21192129
// type
21202130
auto T = def->getType();
21212131

2122-
auto loc = is_defn ? def->getLocation() : VD->getLocation();
2132+
auto loc = is_defn ? def->getSourceRange() : VD->getSourceRange();
2133+
auto &Mgr = Context->getSourceManager();
2134+
auto charRange = clang::CharSourceRange::getCharRange(loc);
2135+
// Extend the range to include the entire final token.
2136+
charRange.setEnd(clang::Lexer::getLocForEndOfToken(loc.getEnd(), 0, Mgr, Context->getLangOpts()));
2137+
loc = charRange.getAsRange();
21232138

21242139
encode_entry(
21252140
VD, TagVarDecl, loc, childIds, T,

0 commit comments

Comments
 (0)