[PATCH] D156378: [clang][CGExprConstant] handle unary negation on integrals

Nick Desaulniers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 26 15:08:07 PDT 2023


nickdesaulniers created this revision.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156378

Files:
  clang/lib/CodeGen/CGExprConstant.cpp


Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1363,6 +1363,19 @@
     return Visit(E->getSubExpr(), T);
   }
 
+  llvm::Constant *VisitUnaryOperator(UnaryOperator *U, QualType T) {
+    switch (U->getOpcode()) {
+    default:
+      break;
+    case UO_Minus:
+      if (llvm::Constant *C = Visit(U->getSubExpr(), T))
+        if (auto *CI = dyn_cast<llvm::ConstantInt>(C))
+          return llvm::ConstantInt::get(CGM.getLLVMContext(), -CI->getValue());
+      break;
+    }
+    return nullptr;
+  }
+
   // Utility methods
   llvm::Type *ConvertType(QualType T) {
     return CGM.getTypes().ConvertType(T);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156378.544536.patch
Type: text/x-patch
Size: 762 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230726/4cc8e48a/attachment.bin>


More information about the cfe-commits mailing list