Skip to content

Commit fecc97d

Browse files
vgvassilevAxel-Naumannjalopezg-git
authored andcommitted
Implement implicit auto keyword injection in clang itself.
Co-authored-by: Axel Naumann <Axel.Naumann@cern.ch> Co-authored-by: Javier Lopez-Gomez <javier.lopez.gomez@cern.ch>
1 parent 07781c4 commit fecc97d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15654,6 +15654,46 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,
1565415654

1565515655
switch (Opc) {
1565615656
case BO_Assign:
15657+
// ROOT hack: we want to support constructs like n = new TNamed() and if n
15658+
// wasn't declared we should declare it.
15659+
if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(LHSExpr)) {
15660+
if (VarDecl* VD = dyn_cast<VarDecl>(DRE->getDecl())) {
15661+
if (const AutoType* aTy = dyn_cast<AutoType>(VD->getType().getTypePtr())) {
15662+
if (const AnnotateAttr* A = VD->getAttr<AnnotateAttr>()) {
15663+
// If the deduction didn't take place and it is our special
15664+
// annotation
15665+
if (!aTy->isDeduced() && A->getAnnotation().equals("__Auto")) {
15666+
QualType ResTy;
15667+
ASTContext& C = getASTContext();
15668+
TypeSourceInfo* TrivialTSI
15669+
= C.getTrivialTypeSourceInfo(VD->getType());
15670+
TemplateDeductionInfo Info(RHSExpr->getExprLoc());
15671+
TemplateDeductionResult Result =
15672+
DeduceAutoType(TrivialTSI->getTypeLoc(), RHSExpr, ResTy, Info);
15673+
if (Result != TDK_Success && Result != TDK_AlreadyDiagnosed) {
15674+
Diag(VD->getLocation(), diag::err_auto_var_requires_init)
15675+
<< VD->getDeclName() << VD->getType();
15676+
VD->setInvalidDecl();
15677+
15678+
return ExprError();
15679+
}
15680+
if (!ResTy.isNull()) {
15681+
VD->setTypeSourceInfo(C.getTrivialTypeSourceInfo(ResTy));
15682+
VD->setType(ResTy);
15683+
}
15684+
VD->setInit(DefaultLvalueConversion(RHSExpr).get());
15685+
PushOnScopeChains(VD, getCurScope(), /*Add to ctx*/true);
15686+
15687+
// Here we need to return 'something' to make the parser happy.
15688+
// A reference to the decl is semantically closest to what we want.
15689+
return BuildDeclRefExpr(VD, VD->getType(), VK_LValue,
15690+
SourceLocation());
15691+
}
15692+
}
15693+
}
15694+
}
15695+
}
15696+
1565715697
ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType(), Opc);
1565815698
if (getLangOpts().CPlusPlus &&
1565915699
LHS.get()->getObjectKind() != OK_ObjCProperty) {

0 commit comments

Comments
 (0)