]> granicus.if.org Git - clang/commitdiff
Teach APValue printer to print boolean 0 and 1 as 'false' and 'true'. Fix up
authorRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 23 Mar 2012 23:55:39 +0000 (23:55 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 23 Mar 2012 23:55:39 +0000 (23:55 +0000)
some calling code to actually pass in a non-null type, to avoid a crash.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@153358 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Sema/Overload.h
lib/AST/APValue.cpp
lib/Sema/SemaInit.cpp
lib/Sema/SemaOverload.cpp
test/SemaCXX/constexpr-printing.cpp

index 1d53faaa2d8267f50403c662d4177852c3a3c615..d334447edc57a9b007c02962d186d16f5ba0e77c 100644 (file)
@@ -236,7 +236,8 @@ namespace clang {
     
     ImplicitConversionRank getRank() const;
     NarrowingKind getNarrowingKind(ASTContext &Context, const Expr *Converted,
-                                   APValue &ConstantValue) const;
+                                   APValue &ConstantValue,
+                                   QualType &ConstantType) const;
     bool isPointerConversionToBool() const;
     bool isPointerConversionToVoidPointer(ASTContext& Context) const;
     void DebugPrint() const;
index 0b5b3b0a5e5fd7bf34e76ccc4463c59caad2ca35..a31b3c5bfb37c4ae058b2114f6ecc0a795e47144 100644 (file)
@@ -312,7 +312,10 @@ void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
     Out << "<uninitialized>";
     return;
   case APValue::Int:
-    Out << getInt();
+    if (Ty->isBooleanType())
+      Out << (getInt().getBoolValue() ? "true" : "false");
+    else
+      Out << getInt();
     return;
   case APValue::Float:
     Out << GetApproxValue(getFloat());
index 78a9f89d598928d839e9becfa86fc75743b2390a..570b2403324076d9c230e0e9a81f7bb59d0ac97b 100644 (file)
@@ -6045,7 +6045,9 @@ static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq,
 
   // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion.
   APValue ConstantValue;
-  switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue)) {
+  QualType ConstantType;
+  switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue,
+                                ConstantType)) {
   case NK_Not_Narrowing:
     // No narrowing occurred.
     return;
@@ -6074,7 +6076,7 @@ static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq,
              diag::err_init_list_constant_narrowing_sfinae
            : diag::err_init_list_constant_narrowing)
       << PostInit->getSourceRange()
-      << ConstantValue.getAsString(S.getASTContext(), EntityType)
+      << ConstantValue.getAsString(S.getASTContext(), ConstantType)
       << EntityType.getLocalUnqualifiedType();
     break;
 
index ff227eee6e17aa73bf2a50c240097646661e5af9..be0243403b5e623fc3971e2c003f34e9a9fc7d27 100644 (file)
@@ -288,10 +288,13 @@ static const Expr *IgnoreNarrowingConversion(const Expr *Converted) {
 /// \param Converted  The result of applying this standard conversion sequence.
 /// \param ConstantValue  If this is an NK_Constant_Narrowing conversion, the
 ///        value of the expression prior to the narrowing conversion.
+/// \param ConstantType  If this is an NK_Constant_Narrowing conversion, the
+///        type of the expression prior to the narrowing conversion.
 NarrowingKind
 StandardConversionSequence::getNarrowingKind(ASTContext &Ctx,
                                              const Expr *Converted,
-                                             APValue &ConstantValue) const {
+                                             APValue &ConstantValue,
+                                             QualType &ConstantType) const {
   assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++");
 
   // C++11 [dcl.init.list]p7:
@@ -325,6 +328,7 @@ StandardConversionSequence::getNarrowingKind(ASTContext &Ctx,
         // If the resulting value is different, this was a narrowing conversion.
         if (IntConstantValue != ConvertedValue) {
           ConstantValue = APValue(IntConstantValue);
+          ConstantType = Initializer->getType();
           return NK_Constant_Narrowing;
         }
       } else {
@@ -354,8 +358,10 @@ StandardConversionSequence::getNarrowingKind(ASTContext &Ctx,
           llvm::APFloat::rmNearestTiesToEven, &ignored);
         // If there was no overflow, the source value is within the range of
         // values that can be represented.
-        if (ConvertStatus & llvm::APFloat::opOverflow)
+        if (ConvertStatus & llvm::APFloat::opOverflow) {
+          ConstantType = Initializer->getType();
           return NK_Constant_Narrowing;
+        }
       } else {
         return NK_Variable_Narrowing;
       }
@@ -400,8 +406,10 @@ StandardConversionSequence::getNarrowingKind(ASTContext &Ctx,
         ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth());
         ConvertedValue.setIsSigned(InitializerValue.isSigned());
         // If the result is different, this was a narrowing conversion.
-        if (ConvertedValue != InitializerValue)
+        if (ConvertedValue != InitializerValue) {
+          ConstantType = Initializer->getType();
           return NK_Constant_Narrowing;
+        }
       } else {
         // Variables are always narrowings.
         return NK_Variable_Narrowing;
@@ -4789,8 +4797,10 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T,
 
   // Check for a narrowing implicit conversion.
   APValue PreNarrowingValue;
+  QualType PreNarrowingType;
   bool Diagnosed = false;
-  switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue)) {
+  switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue,
+                                PreNarrowingType)) {
   case NK_Variable_Narrowing:
     // Implicit conversion to a narrower type, and the value is not a constant
     // expression. We'll diagnose this in a moment.
@@ -4800,7 +4810,7 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T,
   case NK_Constant_Narrowing:
     Diag(From->getLocStart(), diag::err_cce_narrowing)
       << CCE << /*Constant*/1
-      << PreNarrowingValue.getAsString(Context, QualType()) << T;
+      << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T;
     Diagnosed = true;
     break;
 
index 2e0eb9c23d113d0227daf2f67d84115c970e04dc..4e5bc429dbdd5b1f2a5a6a3c5d95809c38a28cc1 100644 (file)
@@ -96,3 +96,7 @@ void LabelDiffTest() {
   static_assert(mulBy3((LabelDiffTy)&&a-(LabelDiffTy)&&b) == 3, ""); // expected-error {{constant expression}} expected-note {{call to 'mulBy3(&&a - &&b)'}}
   a:b:return;
 }
+
+constexpr bool test_bool_printing(bool b) { return 1 / !(2*b | !(2*b)); } // expected-note 2{{division by zero}}
+constexpr bool test_bool_0 = test_bool_printing(false); // expected-error {{constant expr}} expected-note {{in call to 'test_bool_printing(false)'}}
+constexpr bool test_bool_1 = test_bool_printing(true); // expected-error {{constant expr}} expected-note {{in call to 'test_bool_printing(true)'}}