DILocation get wrong column

I wanna locate variable f1 with line and column. But I got wrong column.

// cpp code
    f1 = f1 + 2.2; // line 9

// IR Metadata DILocation
!865 = !DILocation(line: 9, column: 10, scope: !858)  // second f1
!867 = !DILocation(line: 9, column: 8, scope: !858)  // first f1, which should be (9,5)

The second column of f1 should be 5, but I got 8 from Clang. Why? And how can I locate variables presisely?


The DILocation metadata records the source location for an LLVM-IR instruction: it’s usually the line and column where an expression is evaluated. Assuming the LLVM-IR isn’t optimised, you’ll probably find that the first DILocation is for the instruction that loads f1 from the stack, and the second DILocation is for the store corresponding to the = operator.

If you’re looking for the memory location of a variable, you should find a llvm.dbg.declare intrinsic call that is passed the alloca for each variable and the DILocalVariable metadata for that variable. More details in the SourceLevelDebugging documentation. You can find the location where the variable is declared in DILocalVariable.

1 Like

Yes, you are totally right! And the considerate answer is really helpful.

But the DILocalVariable just show the line and column of the first occurence of the variable. What I want is the lines and columns of every occurence.

In that case, you might need to analyse the clang AST rather than the LLVM-IR. Debug-info doesn’t track every use of a variable in an expression – the nearest equivalent might be examining the DILocation of every load and store of the variable’s stack slot, that would broadly correspond with reads / assignments of the variable. However, clang might have already transformed the program when producing LLVM-IR (it might delete unused expressions, perhaps), so the information might not be complete.

1 Like