Hello LLVM Community,
I'm working on some analyzer\checker and want to implement checker for
variables.
In that case, I want to retrieve some information about types of
variables or identify that the variable is either string or not.
As an example, for strings which can be char or string type I came up
with a solution to find it out via isString or isCString methods. But
for strings whose type is wchar my approach is not working. Cause,
internally in IR wchar string is an array of integers.
I looked up some information about how to get types of variables that
were got from the source level in IR level. (I've been reading some info
in LLVM Blog about metadata, in llvm docs about SourceLevel Debugging
with LLVM + StripSymbols.cpp, Metadata.cpp, DebugInfo.cpp)
I found that I can get it from DebugInfo or Metadata.
But my little research was not successful.
my code excerpt:
DebugInfoFinder DIFinder;
DIFinder.processModule(*M);
llvm::outs() << "Count of global Variables in Module : "
<<
DIFinder.global_variable_count() << "\n";
for(DIGlobalVariableExpression *DIGVExpr : DIFinder.global_variables())
{
if(DIGlobalVariable *DIGV = DIGVExpr->getVariable())
{
llvm::outs() << "DIGV DisplayName : " <<
DIGV->getDisplayName() << "\n";
}
}
for(DIType *DIT : DIFinder.types())
{
if(DIBasicType* DIBT = dyn_cast<DIBasicType>(DIT))
{
llvm::outs() << "DIBasicType in Module : " <<
DIT->getName()
<< "\n";
StringRef Encoding
dwarf::AttributeEncodingString(DIBT->getEncoding());
if(!Encoding.empty())
{
llvm::outs() << "Encoding : " <<
Encoding.str() << "\n";
}
}
}
llvm::outs() << "\n\nPrint All Strings Declarations: \n";
for(GlobalVariable& GV : M->globals())
{
if(!GV.hasInitializer()) continue;
if(ConstantDataSequential *CDS
dyn_cast<ConstantDataSequential>(GV.getInitializer()))
{
llvm::outs() << "GVName: " << GV.getName()
<< "\n";
llvm::outs() << "\tRawDataValues : " <<
CDS->getRawDataValues().str() << "\n"; // here I see my
wchar string
if(CDS->isString() || CDS->isCString()) // but these checks
are not appropriate for checking strings of wchar's type
{
llvm::outs() << "\tCString&&Std::String :
" <<
CDS->getAsString().str() << "\n";
}
}
}
What way can I get DIBasicType for ConstantDataSequential?
Literally, I want to know is there any way to get the relationship
between constant, globals and related debuginfo, metadata information?
I would appreciate it for any help.
In advance, thank you very much.
--
----------------------
the best regards!
0x0859549A
> On May 22, 2017, at 6:14 AM, DES via llvm-dev <llvm-dev at lists.llvm.org> wrote: > > What way can I get DIBasicType for ConstantDataSequential? > Literally, I want to know is there any way to get the relationship > between constant, globals and related debuginfo, metadata information?To get to the source variables associated with a global you can use code like this (copied from the IR Verifier): SmallVector<MDNode *, 1> MDs; GV.getMetadata(LLVMContext::MD_dbg, MDs); for (auto *MD : MDs) { if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD)) // do stuff. -- adrian> > I would appreciate it for any help. > In advance, thank you very much. > > -- > ---------------------- > the best regards! > 0x0859549A > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
I'll ask again that question.
Thank you Adrian, I've looked through IR Verifier and it helps me to
understand some parts of retrieving metadata and debug info. But , after
that, I've faced with another problem:
Original Code:
std::wcout << L"WCHAR_HELLOWORLD" << std::endl;b
IR Code:
@.str.2 = private unnamed_addr constant [17 x i32] [i32 87, i32 67, i32
72, i32 65, i32 82, i32 95, i32 72, i32 69, i32 76, i32 76, i32 79, i32
87, i32 79, i32 82, i32 76, i32 68, i32 0], align 4
For that kind of Constant metadata is empty. The way, I could get
metadata as mentioned in Verifier , is not working for Constants that
are not created as Local Variables. I've tried to get the info via Users
of that Value, but it wasn't working either.
%call39 = invoke dereferenceable(160)
%"class.std::__1::basic_ostream.13"*
@_ZNSt3__1lsIwNS_11char_traitsIwEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_(%"class.std::__1::basic_ostream.13"*
dereferenceable(160) @_ZNSt3__15wcoutE, i32* getelementptr inbounds ([17
x i32], [17 x i32]* @.str.2, i32 0, i32 0))
to label %invoke.cont38 unwind label %lpad5, !dbg !5551
I'm digging around Verifier, Metadata, DebugInfo, though, it comes to me
that I miss something
Is it some design decision that isString method does not return TRUE
for wide-character string?
Should I do some additional analysis for constants(like collect meta for
all instructions in all functions and build a map after)?
Who knows sources where I could find some examples dealing with
constants' metadata?
And again, in advance, thank you very much!
----------------------
the best regards!
0x0859549A
On 05/22/2017 08:45 PM, Adrian Prantl wrote:>> On May 22, 2017, at 6:14 AM, DES via llvm-dev <llvm-dev at
lists.llvm.org> wrote:
>>
>> What way can I get DIBasicType for ConstantDataSequential?
>> Literally, I want to know is there any way to get the relationship
>> between constant, globals and related debuginfo, metadata information?
> To get to the source variables associated with a global you can use code
like this (copied from the IR Verifier):
>
> SmallVector<MDNode *, 1> MDs;
> GV.getMetadata(LLVMContext::MD_dbg, MDs);
> for (auto *MD : MDs) {
> if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD))
> // do stuff.
>
> -- adrian
>
>> I would appreciate it for any help.
>> In advance, thank you very much.
>>
>> --
>> ----------------------
>> the best regards!
>> 0x0859549A
>>
>>
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev