diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index a0484bc35e13..a642fd236d38 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -296,6 +296,10 @@ public: static_cast(CGM.getContext().getTypeSize(E->getType())); return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); } + + llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { + return Visit(E->getSubExpr()); + } // Binary operators llvm::Constant *VisitBinOr(const BinaryOperator *E) { @@ -380,6 +384,37 @@ public: return llvm::ConstantExpr::getAnd(LHS, RHS); } + + llvm::Constant *VisitBinNE(const BinaryOperator *E) { + llvm::Constant *LHS = Visit(E->getLHS()); + llvm::Constant *RHS = Visit(E->getRHS()); + + const llvm::Type* ResultType = ConvertType(E->getType()); + if (!ResultType->isInteger()) { + CGM.WarnUnsupported(E, "constant expression"); + return llvm::Constant::getNullValue(ConvertType(E->getType())); + } + llvm::Constant *Result = + llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, LHS, RHS); + return llvm::ConstantExpr::getZExtOrBitCast(Result, ResultType); + } + + llvm::Constant *VisitConditionalOperator(const ConditionalOperator *E) { + llvm::Constant *Cond = Visit(E->getCond()); + llvm::Constant *CondVal = EmitConversionToBool(Cond, E->getType()); + llvm::ConstantInt *CondValInt = dyn_cast(CondVal); + if (!CondValInt) { + CGM.WarnUnsupported(E, "constant expression"); + return llvm::Constant::getNullValue(ConvertType(E->getType())); + } + if (CondValInt->isOne()) { + if (E->getLHS()) + return Visit(E->getLHS()); + return Cond; + } + + return Visit(E->getRHS()); + } // Utility methods const llvm::Type *ConvertType(QualType T) { diff --git a/clang/test/CodeGen/const-init.c b/clang/test/CodeGen/const-init.c index e3f110d64ed8..ccc34cf13684 100644 --- a/clang/test/CodeGen/const-init.c +++ b/clang/test/CodeGen/const-init.c @@ -1,4 +1,4 @@ -// RUN: clang -emit-llvm %s +// RUN: clang -emit-llvm %s 2>&1 | not grep warning #include @@ -11,3 +11,5 @@ intptr_t b = a; int c(); void *d = c; intptr_t e = c; + +int f, *g = __extension__ &f, *h = (1 != 1) ? &f : &f;