[LLVMdev] LLVM internal getDirectory() for LexicalBlock debug information returns filename?

Martijn martijn at martijnrutten.com
Wed Apr 27 00:57:08 PDT 2011


Hi,

For my project, I am using internal llvm class functions to print the
filename and directory associated with an instruction from the debug
information.
With a recent checkout of llvm and clang (svn revision 129445), the
following custom code (e.g. in a pass that prints directory info for
all instructions) returns the filename iso. directory whenever Scope
refers to a lexical block:

if (const Instruction *Inst = dyn_cast<Instruction>(&I)) {
  const DebugLoc &Loc = Inst->getDebugLoc();
  if (!Loc.isUnknown()) {
    LLVMContext &Ctx = Inst->getParent()->getParent()->getContext();
    DIScope Scope(Loc.getScope(Ctx));
    StringRef Dir = Scope.getDirectory(); // Wrong: contains filename
iso. directory

    ....
  }
}

Here, if Scope refers to a Subprogram, this works correctly. However,
if Scope is a LexicalBlock, getDirectory() returns the filename
(conv.c) iso. the directory. The associated .ll does have a reference
to the proper directory (/home1/martijn/hello) in !1 referred from
!33:

  ...
  store i32 %0, i32* %px.addr.09, align 4, !dbg !33, !tbaa !36
  ...

  !0 = metadata !{i32 589870, i32 0, metadata !1, metadata !"init",
metadata !"init", metadata !"", metadata !1, i32 9, metadata !3, i1
false, i1 true, i32 0, i32 0, i32 0, i32 256, i1 true, void (i32*,
i32*)* @init, null} ; [ DW_TAG_subprogram ]
  !1 = metadata !{i32 589865, metadata !"conv.c", metadata
!"/home1/martijn/hello", metadata !2} ; [ DW_TAG_file_type ]
  !2 = metadata !{i32 589841, i32 0, i32 12, metadata !"conv.c",
metadata !"/home1/martijn/hello", metadata !"clang version 3.0
(trunk)", i1 true, i1 true, metadata !"", i32 0} ; [
DW_TAG_compile_unit ]
  ...
  !14 = metadata !{i32 589835, metadata !0, i32 9, i32 1, metadata !1,
i32 0} ; [ DW_TAG_lexical_block ]
  ...
  !33 = metadata !{i32 13, i32 5, metadata !34, null}
  !34 = metadata !{i32 589835, metadata !35, i32 12, i32 3, metadata
!1, i32 2} ; [ DW_TAG_lexical_block ]
  !35 = metadata !{i32 589835, metadata !14, i32 11, i32 3, metadata
!1, i32 1} ; [ DW_TAG_lexical_block ]
  ...

In llvm/Analysis/DebugInfo.h, I see some exception code in
DISubProgram for LLVMDebugVersion7:

    StringRef getDirectory() const   {
      if (getVersion() == llvm::LLVMDebugVersion7)
        return getCompileUnit().getFilename();

      return getFieldAs<DIFile>(6).getDirectory();
    }

The DILexicalBlock code does not include such exceptions, but even if
I try to add such code, the result is still the filename iso
directory.

I am at a loss how to address this. Any help would be greatly appreciated!

Regards,
  Martijn Rutten



More information about the llvm-dev mailing list