[cfe-commits] r126321 - in /cfe/trunk: include/clang/Sema/DeclSpec.h lib/Parse/ParseDecl.cpp lib/Sema/SemaType.cpp test/Sema/return.c test/SemaCXX/return.cpp

Chandler Carruth chandlerc at gmail.com
Wed Feb 23 10:51:59 PST 2011


Author: chandlerc
Date: Wed Feb 23 12:51:59 2011
New Revision: 126321

URL: http://llvm.org/viewvc/llvm-project?rev=126321&view=rev
Log:
Fix the behavior of -Wignored-qualifiers on return type qualifiers in
several ways. We now warn for more of the return types, and correctly
locate the ignored ones. Also adds fix-it hints to remove the ignored
qualifiers. Fixes much of PR9058, although not all of it.

Patch by Hans Wennborg, a couple of minor style tweaks from me.

Modified:
    cfe/trunk/include/clang/Sema/DeclSpec.h
    cfe/trunk/lib/Parse/ParseDecl.cpp
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/Sema/return.c
    cfe/trunk/test/SemaCXX/return.cpp

Modified: cfe/trunk/include/clang/Sema/DeclSpec.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/DeclSpec.h?rev=126321&r1=126320&r2=126321&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/DeclSpec.h (original)
+++ cfe/trunk/include/clang/Sema/DeclSpec.h Wed Feb 23 12:51:59 2011
@@ -825,6 +825,16 @@
   struct PointerTypeInfo : TypeInfoCommon {
     /// The type qualifiers: const/volatile/restrict.
     unsigned TypeQuals : 3;
+
+    /// The location of the const-qualifier, if any.
+    unsigned ConstQualLoc;
+
+    /// The location of the volatile-qualifier, if any.
+    unsigned VolatileQualLoc;
+
+    /// The location of the restrict-qualifier, if any.
+    unsigned RestrictQualLoc;
+
     void destroy() {
     }
   };
@@ -1055,12 +1065,18 @@
   /// getPointer - Return a DeclaratorChunk for a pointer.
   ///
   static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc,
+                                    SourceLocation ConstQualLoc,
+                                    SourceLocation VolatileQualLoc,
+                                    SourceLocation RestrictQualLoc,
                                     const ParsedAttributes &attrs) {
     DeclaratorChunk I;
-    I.Kind          = Pointer;
-    I.Loc           = Loc;
-    I.Ptr.TypeQuals = TypeQuals;
-    I.Ptr.AttrList  = attrs.getList();
+    I.Kind                = Pointer;
+    I.Loc                 = Loc;
+    I.Ptr.TypeQuals       = TypeQuals;
+    I.Ptr.ConstQualLoc    = ConstQualLoc.getRawEncoding();
+    I.Ptr.VolatileQualLoc = VolatileQualLoc.getRawEncoding();
+    I.Ptr.RestrictQualLoc = RestrictQualLoc.getRawEncoding();
+    I.Ptr.AttrList        = attrs.getList();
     return I;
   }
 

Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=126321&r1=126320&r2=126321&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Wed Feb 23 12:51:59 2011
@@ -2762,6 +2762,9 @@
     if (Kind == tok::star)
       // Remember that we parsed a pointer type, and remember the type-quals.
       D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
+                                                DS.getConstSpecLoc(),
+                                                DS.getVolatileSpecLoc(),
+                                                DS.getRestrictSpecLoc(),
                                                 DS.takeAttributes()),
                     SourceLocation());
     else
@@ -3758,4 +3761,3 @@
   }
   return false;
 }
-  

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=126321&r1=126320&r2=126321&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Wed Feb 23 12:51:59 2011
@@ -1395,6 +1395,56 @@
   return QT;
 }
 
+static void DiagnoseIgnoredQualifiers(unsigned Quals,
+                                      SourceLocation ConstQualLoc,
+                                      SourceLocation VolatileQualLoc,
+                                      SourceLocation RestrictQualLoc,
+                                      Sema& S) {
+  std::string QualStr;
+  unsigned NumQuals = 0;
+  SourceLocation Loc;
+
+  FixItHint ConstFixIt;
+  FixItHint VolatileFixIt;
+  FixItHint RestrictFixIt;
+
+  // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
+  // find a range and grow it to encompass all the qualifiers, regardless of
+  // the order in which they textually appear.
+  if (Quals & Qualifiers::Const) {
+    ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
+    Loc = ConstQualLoc;
+    ++NumQuals;
+    QualStr = "const";
+  }
+  if (Quals & Qualifiers::Volatile) {
+    VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
+    if (NumQuals == 0) {
+      Loc = VolatileQualLoc;
+      QualStr = "volatile";
+    } else {
+      QualStr += " volatile";
+    }
+    ++NumQuals;
+  }
+  if (Quals & Qualifiers::Restrict) {
+    RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
+    if (NumQuals == 0) {
+      Loc = RestrictQualLoc;
+      QualStr = "restrict";
+    } else {
+      QualStr += " restrict";
+    }
+    ++NumQuals;
+  }
+
+  assert(NumQuals > 0 && "No known qualifiers?");
+
+  S.Diag(Loc, diag::warn_qual_return_type)
+    << QualStr << NumQuals
+    << ConstFixIt << VolatileFixIt << RestrictFixIt;
+}
+
 /// GetTypeForDeclarator - Convert the type for the specified
 /// declarator to Type instances.
 ///
@@ -1678,46 +1728,31 @@
 
       // cv-qualifiers on return types are pointless except when the type is a
       // class type in C++.
-      if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
+      if (T->isPointerType() && T.getCVRQualifiers() &&
+          (!getLangOptions().CPlusPlus || !T->isDependentType())) {
+        assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
+        DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
+        assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
+
+        DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
+
+        DiagnoseIgnoredQualifiers(PTI.TypeQuals,
+            SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
+            SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
+            SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
+            *this);
+
+      } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
           (!getLangOptions().CPlusPlus ||
            (!T->isDependentType() && !T->isRecordType()))) {
-        unsigned Quals = D.getDeclSpec().getTypeQualifiers();
-        std::string QualStr;
-        unsigned NumQuals = 0;
-        SourceLocation Loc;
-        if (Quals & Qualifiers::Const) {
-          Loc = D.getDeclSpec().getConstSpecLoc();
-          ++NumQuals;
-          QualStr = "const";
-        }
-        if (Quals & Qualifiers::Volatile) {
-          if (NumQuals == 0) {
-            Loc = D.getDeclSpec().getVolatileSpecLoc();
-            QualStr = "volatile";
-          } else
-            QualStr += " volatile";
-          ++NumQuals;
-        }
-        if (Quals & Qualifiers::Restrict) {
-          if (NumQuals == 0) {
-            Loc = D.getDeclSpec().getRestrictSpecLoc();
-            QualStr = "restrict";
-          } else
-            QualStr += " restrict";
-          ++NumQuals;
-        }
-        assert(NumQuals > 0 && "No known qualifiers?");
-            
-        SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type);
-        DB << QualStr << NumQuals;
-        if (Quals & Qualifiers::Const)
-          DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc());
-        if (Quals & Qualifiers::Volatile)
-          DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc());
-        if (Quals & Qualifiers::Restrict)
-          DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc());
+
+        DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
+                                  D.getDeclSpec().getConstSpecLoc(),
+                                  D.getDeclSpec().getVolatileSpecLoc(),
+                                  D.getDeclSpec().getRestrictSpecLoc(),
+                                  *this);
       }
-      
+
       if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
         // C++ [dcl.fct]p6:
         //   Types shall not be defined in return or parameter types.

Modified: cfe/trunk/test/Sema/return.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/return.c?rev=126321&r1=126320&r2=126321&view=diff
==============================================================================
--- cfe/trunk/test/Sema/return.c (original)
+++ cfe/trunk/test/Sema/return.c Wed Feb 23 12:51:59 2011
@@ -242,6 +242,7 @@
 // Test warnings on ignored qualifiers on return types.
 const int ignored_c_quals(); // expected-warning{{'const' type qualifier on return type has no effect}}
 const volatile int ignored_cv_quals(); // expected-warning{{'const volatile' type qualifiers on return type have no effect}}
+char* const volatile restrict ignored_cvr_quals(); // expected-warning{{'const volatile restrict' type qualifiers on return type have no effect}}
 
 // Test that for switch(enum) that if the switch statement covers all the cases
 // that we don't consider that for -Wreturn-type.
@@ -254,4 +255,3 @@
   case C3: return 4;
   }
 } // no-warning
-

Modified: cfe/trunk/test/SemaCXX/return.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/return.cpp?rev=126321&r1=126320&r2=126321&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/return.cpp (original)
+++ cfe/trunk/test/SemaCXX/return.cpp Wed Feb 23 12:51:59 2011
@@ -24,5 +24,20 @@
 const volatile S class_cv();
 
 const int scalar_c(); // expected-warning{{'const' type qualifier on return type has no effect}}
+int const scalar_c2(); // expected-warning{{'const' type qualifier on return type has no effect}}
+
+const
+char*
+const // expected-warning{{'const' type qualifier on return type has no effect}}
+f();
+
+char
+const*
+const // expected-warning{{'const' type qualifier on return type has no effect}}
+g();
+
+char* const h(); // expected-warning{{'const' type qualifier on return type has no effect}}
+char* volatile i(); // expected-warning{{'volatile' type qualifier on return type has no effect}}
+
 const volatile int scalar_cv(); // expected-warning{{'const volatile' type qualifiers on return type have no effect}}
 }





More information about the cfe-commits mailing list