[clang] 769333a - [clang][CGExprConstant] handle unary negation on integrals

Nick Desaulniers via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 7 14:51:08 PDT 2023


Author: Nick Desaulniers
Date: 2023-08-07T14:51:01-07:00
New Revision: 769333aeeb4508a39ce61cde5d70d880f9abfc35

URL: https://github.com/llvm/llvm-project/commit/769333aeeb4508a39ce61cde5d70d880f9abfc35
DIFF: https://github.com/llvm/llvm-project/commit/769333aeeb4508a39ce61cde5d70d880f9abfc35.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprConstant.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index 9ad07f7d2220a7..e482b57eb144d7 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -1363,6 +1363,13 @@ class ConstExprEmitter :
     return Visit(E->getSubExpr(), T);
   }
 
+  llvm::Constant *VisitUnaryMinus(UnaryOperator *U, QualType T) {
+    if (llvm::Constant *C = Visit(U->getSubExpr(), T))
+      if (auto *CI = dyn_cast<llvm::ConstantInt>(C))
+        return llvm::ConstantInt::get(CGM.getLLVMContext(), -CI->getValue());
+    return nullptr;
+  }
+
   // Utility methods
   llvm::Type *ConvertType(QualType T) {
     return CGM.getTypes().ConvertType(T);


        


More information about the cfe-commits mailing list