Skip to content

Commit 769333a

Browse files
[clang][CGExprConstant] handle unary negation on integrals
Consider the statement: int x = -1; And the following AST: `-VarDecl 0x55c4823a7670 <x.c:2:1, col:10> col:5 x 'int' cinit `-UnaryOperator 0x55c4823a7740 <col:9, col:10> 'int' prefix '-' `-IntegerLiteral 0x55c4823a7720 <col:10> 'int' 1 Return the evaluation of the subexpression negated. Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D156378
1 parent f6ba352 commit 769333a

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,13 @@ class ConstExprEmitter :
13631363
return Visit(E->getSubExpr(), T);
13641364
}
13651365

1366+
llvm::Constant *VisitUnaryMinus(UnaryOperator *U, QualType T) {
1367+
if (llvm::Constant *C = Visit(U->getSubExpr(), T))
1368+
if (auto *CI = dyn_cast<llvm::ConstantInt>(C))
1369+
return llvm::ConstantInt::get(CGM.getLLVMContext(), -CI->getValue());
1370+
return nullptr;
1371+
}
1372+
13661373
// Utility methods
13671374
llvm::Type *ConvertType(QualType T) {
13681375
return CGM.getTypes().ConvertType(T);

0 commit comments

Comments
 (0)