[cfe-dev] Understanding Source Locations
Stephen Kelly via cfe-dev
cfe-dev at lists.llvm.org
Mon Feb 18 12:09:59 PST 2019
On 15/02/2019 23:07, Alex Denisov via cfe-dev wrote:
> 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
Hi Alex,
I couldn't repro this with clang-query at least. Perhaps if you provide
an sscce.org, it will reveal the problem.
Thanks,
Stephen.
More information about the cfe-dev
mailing list