Example #1
0
    bool VisitCompoundStmt(CompoundStmt* CS) {
      for(CompoundStmt::body_iterator I = CS->body_begin(), E = CS->body_end();
          I != E; ++I) {
        if (!isa<BinaryOperator>(*I))
          continue;

        const BinaryOperator* BinOp = cast<BinaryOperator>(*I);
        if (isAutoCandidate(BinOp)) {
          ASTContext& C = m_Sema->getASTContext();
          VarDecl* VD 
            = cast<VarDecl>(cast<DeclRefExpr>(BinOp->getLHS())->getDecl());
          TypeSourceInfo* ResTSI = 0;
          TypeSourceInfo* TrivialTSI 
            = C.getTrivialTypeSourceInfo(VD->getType());
          Expr* RHS = BinOp->getRHS();
          m_Sema->DeduceAutoType(TrivialTSI, RHS, ResTSI);
          VD->setTypeSourceInfo(ResTSI);
          VD->setType(ResTSI->getType());
          VD->setInit(RHS);
          Sema::DeclGroupPtrTy VDPtrTy = m_Sema->ConvertDeclToDeclGroup(VD);
          // Transform the AST into a "sane" state. Replace the binary operator
          // with decl stmt, because the binop semantically is a decl with init.
          StmtResult DS = m_Sema->ActOnDeclStmt(VDPtrTy, BinOp->getLocStart(), 
                                                BinOp->getLocEnd());
          assert(!DS.isInvalid() && "Invalid DeclStmt.");
          *I = DS.take();
        }
      }
      return true; // returning false will abort the in-depth traversal.
    }
Example #2
0
 void Fix(CompoundStmt* CS) {
   if (!CS->size())
     return;
   typedef llvm::SmallVector<Stmt*, 32> Statements;
   Statements Stmts;
   Stmts.append(CS->body_begin(), CS->body_end());
   for (Statements::iterator I = Stmts.begin(); I != Stmts.end(); ++I) {
     if (!TraverseStmt(*I) && !m_HandledDecls.count(m_FoundDRE->getDecl())) {
       Sema::DeclGroupPtrTy VDPtrTy 
         = m_Sema->ConvertDeclToDeclGroup(m_FoundDRE->getDecl());
       StmtResult DS = m_Sema->ActOnDeclStmt(VDPtrTy, 
                                             m_FoundDRE->getLocStart(), 
                                             m_FoundDRE->getLocEnd());
       assert(!DS.isInvalid() && "Invalid DeclStmt.");
       I = Stmts.insert(I, DS.take());
       m_HandledDecls.insert(m_FoundDRE->getDecl());
     }
   }
   CS->setStmts(m_Sema->getASTContext(), Stmts.data(), Stmts.size());
 }