[cfe-commits] r118773 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Sema/SemaChecking.cpp lib/Sema/SemaInit.cpp test/Sema/constant-conversion.c test/SemaCXX/conversion.cpp

John McCall rjmccall at apple.com
Wed Nov 10 19:21:53 PST 2010


Author: rjmccall
Date: Wed Nov 10 21:21:53 2010
New Revision: 118773

URL: http://llvm.org/viewvc/llvm-project?rev=118773&view=rev
Log:
Extend the bitfield-truncation warning to initializations.
rdar://problem/8652606


Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaInit.cpp
    cfe/trunk/test/Sema/constant-conversion.c
    cfe/trunk/test/SemaCXX/conversion.cpp

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=118773&r1=118772&r2=118773&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Wed Nov 10 21:21:53 2010
@@ -4539,6 +4539,9 @@
   void CheckFloatComparison(SourceLocation loc, Expr* lex, Expr* rex);
   void CheckImplicitConversions(Expr *E, SourceLocation CC = SourceLocation());
 
+  void CheckBitFieldInitialization(SourceLocation InitLoc, FieldDecl *Field,
+                                   Expr *Init);
+
   /// \brief The parser's current scope.
   ///
   /// The parser maintains this state here.
@@ -4546,6 +4549,7 @@
   
 protected:
   friend class Parser;
+  friend class InitializationSequence;
   
   /// \brief Retrieve the parser's current scope.
   Scope *getCurScope() const { return CurScope; }  

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=118773&r1=118772&r2=118773&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Nov 10 21:21:53 2010
@@ -2608,6 +2608,55 @@
     << lex->getSourceRange() << rex->getSourceRange();
 }
 
+/// Analyzes an attempt to assign the given value to a bitfield.
+///
+/// Returns true if there was something fishy about the attempt.
+bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
+                               SourceLocation InitLoc) {
+  assert(Bitfield->isBitField());
+  if (Bitfield->isInvalidDecl())
+    return false;
+
+  Expr *OriginalInit = Init->IgnoreParenImpCasts();
+
+  llvm::APSInt Width(32);
+  Expr::EvalResult InitValue;
+  if (!Bitfield->getBitWidth()->isIntegerConstantExpr(Width, S.Context) ||
+      !Init->Evaluate(InitValue, S.Context) ||
+      !InitValue.Val.isInt())
+    return false;
+
+  const llvm::APSInt &Value = InitValue.Val.getInt();
+  unsigned OriginalWidth = Value.getBitWidth();
+  unsigned FieldWidth = Width.getZExtValue();
+
+  if (OriginalWidth <= FieldWidth)
+    return false;
+
+  llvm::APSInt TruncatedValue = Value;
+  TruncatedValue.trunc(FieldWidth);
+
+  // It's fairly common to write values into signed bitfields
+  // that, if sign-extended, would end up becoming a different
+  // value.  We don't want to warn about that.
+  if (Value.isSigned() && Value.isNegative())
+    TruncatedValue.sext(OriginalWidth);
+  else
+    TruncatedValue.zext(OriginalWidth);
+
+  if (Value == TruncatedValue)
+    return false;
+
+  std::string PrettyValue = Value.toString(10);
+  std::string PrettyTrunc = TruncatedValue.toString(10);
+
+  S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
+    << PrettyValue << PrettyTrunc << OriginalInit->getType()
+    << Init->getSourceRange();
+
+  return true;
+}
+
 /// Analyze the given simple or compound assignment for warning-worthy
 /// operations.
 void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
@@ -2617,44 +2666,11 @@
   // We want to recurse on the RHS as normal unless we're assigning to
   // a bitfield.
   if (FieldDecl *Bitfield = E->getLHS()->getBitField()) {
-    assert(Bitfield->isBitField());
-
-    Expr *RHS = E->getRHS()->IgnoreParenImpCasts();
-
-    llvm::APSInt Width(32);
-    Expr::EvalResult RHSValue;
-    if (!Bitfield->isInvalidDecl() &&
-        Bitfield->getBitWidth()->isIntegerConstantExpr(Width, S.Context) &&
-        RHS->Evaluate(RHSValue, S.Context) && RHSValue.Val.isInt()) {
-      const llvm::APSInt &Value = RHSValue.Val.getInt();
-      unsigned OriginalWidth = Value.getBitWidth();
-      unsigned FieldWidth = Width.getZExtValue();
-
-      if (OriginalWidth > FieldWidth) {
-        llvm::APSInt TruncatedValue = Value;
-        TruncatedValue.trunc(FieldWidth);
-
-        // It's fairly common to write values into signed bitfields
-        // that, if sign-extended, would end up becoming a different
-        // value.  We don't want to warn about that.
-        if (Value.isSigned() && Value.isNegative())
-          TruncatedValue.sext(OriginalWidth);
-        else
-          TruncatedValue.zext(OriginalWidth);
-
-        if (Value != TruncatedValue) {
-          std::string PrettyValue = Value.toString(10);
-          std::string PrettyTrunc = TruncatedValue.toString(10);
-
-          S.Diag(E->getOperatorLoc(),
-                 diag::warn_impcast_bitfield_precision_constant)
-            << PrettyValue << PrettyTrunc << RHS->getType()
-            << E->getRHS()->getSourceRange();
-
-          // Recurse, ignoring any implicit conversions on the RHS.
-          return AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
-        }
-      }
+    if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
+                                  E->getOperatorLoc())) {
+      // Recurse, ignoring any implicit conversions on the RHS.
+      return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
+                                        E->getOperatorLoc());
     }
   }
 
@@ -2932,6 +2948,12 @@
   AnalyzeImplicitConversions(*this, E, CC);
 }
 
+void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
+                                       FieldDecl *BitField,
+                                       Expr *Init) {
+  (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
+}
+
 /// CheckParmsForFunctionDef - Check that the parameters of the given
 /// function are appropriate for the definition of a function. This
 /// takes care of any checks that cannot be performed on the

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=118773&r1=118772&r2=118773&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Wed Nov 10 21:21:53 2010
@@ -4047,6 +4047,13 @@
       break;
     }
   }
+
+  // Diagnose non-fatal problems with the completed initialization.
+  if (Entity.getKind() == InitializedEntity::EK_Member &&
+      cast<FieldDecl>(Entity.getDecl())->isBitField())
+    S.CheckBitFieldInitialization(Kind.getLocation(),
+                                  cast<FieldDecl>(Entity.getDecl()),
+                                  CurInit.get());
   
   return move(CurInit);
 }
@@ -4534,7 +4541,7 @@
   if (Init.isInvalid())
     return ExprError();
 
-  Expr *InitE = (Expr *)Init.get();
+  Expr *InitE = Init.get();
   assert(InitE && "No initialization expression?");
 
   if (EqualLoc.isInvalid())

Modified: cfe/trunk/test/Sema/constant-conversion.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/constant-conversion.c?rev=118773&r1=118772&r2=118773&view=diff
==============================================================================
--- cfe/trunk/test/Sema/constant-conversion.c (original)
+++ cfe/trunk/test/Sema/constant-conversion.c Wed Nov 10 21:21:53 2010
@@ -24,3 +24,16 @@
   *t = 20;
   return 10; // shouldn't warn
 }
+
+void test3() {
+  struct A {
+    unsigned int foo : 2;
+    int bar : 2;
+  };
+
+  struct A a = { 0, 10 };            // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
+  struct A b[] = { 0, 10, 0, 0 };    // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
+  struct A c[] = {{10, 0}};          // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
+  struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
+  struct A e = { .foo = 10 };        // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
+}

Modified: cfe/trunk/test/SemaCXX/conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/conversion.cpp?rev=118773&r1=118772&r2=118773&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/conversion.cpp (original)
+++ cfe/trunk/test/SemaCXX/conversion.cpp Wed Nov 10 21:21:53 2010
@@ -43,3 +43,10 @@
     return p == foo();
   }
 }
+
+namespace test2 {
+  struct A {
+    unsigned int x : 2;
+    A() : x(10) {} // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
+  };
+}





More information about the cfe-commits mailing list