When using the Clang C++ API, assuming that I have a QualType
, how would I synthesize a variable declaration of that particular QualType
, this is just to interrogate properties of that declaration then, I don't plan to inject this anywhere.
Went through the particular hierarchy under clang::Decl
but could not really find a clue there...
If you want to synthesize a variable declaration from a QualType
, you can use ASTContext::getTrivialTypeSourceInfo
to create a TypeSourceInfo
for the QualType
, then construct a VarDecl
using VarDecl::Create
with the TypeSourceInfo
and the appropriate declaration context.
One example fragment doing something like this in the Clang sources is in SemaTemplateDeductionGuide.cpp
:
auto *TSI = SemaRef.Context.getTrivialTypeSourceInfo(T, Loc);
if (NestedPattern)
TSI = SemaRef.SubstType(TSI, OuterInstantiationArgs, Loc,
DeclarationName());
if (!TSI)
return nullptr;
ParmVarDecl *NewParam =
ParmVarDecl::Create(SemaRef.Context, DC, Loc, Loc, nullptr,
TSI->getType(), TSI, SC_None, nullptr);