Understanding Source Locations

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());

3. In the visitor, dump locations of some BinaryOperators:

bool VisitBinaryOperator(clang::BinaryOperator *binaryOperator) {
  auto range = binaryOperator->getSourceRange();

It all works good, until some point:


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 15.0.0git documentation
[2] Precompiled Header and Modules Internals — Clang 15.0.0git documentation
[3] Driver Design & Internals — Clang 15.0.0git documentation
[4] header.h · GitHub

Thank you,

main.cpp (584 Bytes)

header.h (148 Bytes)

Hi Alex,

I couldn't repro this with clang-query at least. Perhaps if you provide an sscce.org, it will reveal the problem.



Btw, if anyone faces similar issue: the problem was caused by buildASTFromCodeWithArgs, not by source locations.
For some reason buildASTFromCodeWithArgs did not produce correct AST, presumably because of an include. When I inline the code from header it works as expected.
The workaround (or the proper solution?) is to use clang::ASTUnit::LoadFromCommandLine, which works correctly.

Best regards,