Hi there,
I'm having an issue using SourceLocations.
I do the following:
1. Create ASTUnit from code:
auto buffer = MemoryBuffer::getFile(path);
Twine code(buffer.get()->getBuffer());
auto ast = std::move(clang::tooling::buildASTFromCodeWithArgs(code,
{ "-x", "c++", "-I", "<the right path to a header>"}, path));
2. Create simple custom RecursiveASTVisitor and run it against the ASTUnit:
Visitor visitor(ast->getSourceManager());
visitor.TraverseDecl(ast->getASTContext().getTranslationUnitDecl());
3. In the visitor, dump locations of some BinaryOperators:
bool VisitBinaryOperator(clang::BinaryOperator *binaryOperator) {
auto range = binaryOperator->getSourceRange();
range.getBegin().dump(sourceManager);
}
It all works good, until some point:
include/header.h:8:9
main.cpp:5:7
main.cpp:9:7
main.cpp:13:7
main.cpp:17:7
<marker>
main.cpp:18:126
main.cpp:22:19
main.cpp:32:7
All locations before the <marker> are correct, but then something happens and goes off the track: I certainly do not have 126 characters long lines. The last three locations point to a wrong place, both line- and column-wise.
I must be doing something wrong, but could not figure it out.
I read through the related documentation I could find[1][2][3], but no luck so far. I also tried converting those locations into some other types of locations.
I would appreciate if anyone can give some hints on what could go wrong. I attach the files being parsed to this email, but they can be found here[4] as well.
[1] “Clang” CFE Internals Manual — Clang 16.0.0git documentation
[2] https://clang.llvm.org/docs/PCHInternals.html
[3] https://clang.llvm.org/docs/DriverInternals.html
[4] https://gist.github.com/AlexDenisov/543da61e0ef6250d5f490f2464c52e9b
Thank you,
Alex.
main.cpp (584 Bytes)
header.h (148 Bytes)