]> granicus.if.org Git - clang/commitdiff
Re-enabled support for the Subjects for the weak attribute. This changes the diagnost...
authorAaron Ballman <aaron@aaronballman.com>
Mon, 2 Dec 2013 17:07:07 +0000 (17:07 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Mon, 2 Dec 2013 17:07:07 +0000 (17:07 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@196120 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/Attr.td
include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Sema/AttributeList.h
lib/Sema/SemaDeclAttr.cpp
test/SemaCXX/attr-weak.cpp
utils/TableGen/ClangAttrEmitter.cpp

index e947b8ebfd6f102f888c0254e9b2fa72ad42a250..c7223d65121b860b0edcf0a1f638d0e282ed8f73 100644 (file)
@@ -870,7 +870,7 @@ def WarnUnusedResult : InheritableAttr {
 
 def Weak : InheritableAttr {
   let Spellings = [GNU<"weak">, CXX11<"gnu", "weak">];
-//  let Subjects = SubjectList<[Var, Function, CXXRecord]>;
+  let Subjects = SubjectList<[Var, Function, CXXRecord]>;
 }
 
 def WeakImport : InheritableAttr {
index 995603072c689db4c445b93ea6321846c70c31f9..20cdfdf545f57b5365d35569dbe604843bbcad9b 100644 (file)
@@ -2037,7 +2037,7 @@ def warn_attribute_wrong_decl_type : Warning<
   "variables and fields|variables, data members and tag types|"
   "types and namespaces|Objective-C interfaces|methods and properties|"
   "struct or union|struct, union or class|types|"
-  "Objective-C instance methods}1">,
+  "Objective-C instance methods|variables, functions and classes}1">,
   InGroup<IgnoredAttributes>;
 def err_attribute_wrong_decl_type : Error<
   "%0 attribute only applies to %select{functions|unions|"
@@ -2049,7 +2049,7 @@ def err_attribute_wrong_decl_type : Error<
   "variables and fields|variables, data members and tag types|"
   "types and namespaces|Objective-C interfaces|methods and properties|"
   "struct or union|struct, union or class|types|"
-  "Objective-C instance methods}1">;
+  "Objective-C instance methods|variables, functions and classes}1">;
 def warn_type_attribute_wrong_type : Warning<
   "'%0' only applies to %select{function|pointer|"
   "Objective-C object or block pointer}1 types; type here is %2">,
index 690ccccbef62f895fbb8ac3e876478b8052a8d3f..e27209525e9df156ae0f451302fe3f3edcb6c4a2 100644 (file)
@@ -859,7 +859,8 @@ enum AttributeDeclKind {
   ExpectedStructOrUnion,
   ExpectedStructOrUnionOrClass,
   ExpectedType,
-  ExpectedObjCInstanceMethod
+  ExpectedObjCInstanceMethod,
+  ExpectedFunctionVariableOrClass
 };
 
 }  // end namespace clang
index 1ae62ef3af007724b5cfd209ace6bee0d4f135e2..486375addee150c889832a3130cbe027b412e99d 100644 (file)
@@ -2243,24 +2243,6 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr)
                                   Attr.getAttributeSpellingListIndex()));
 }
 
-static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
-  if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
-    if (isa<CXXRecordDecl>(D)) {
-      D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
-      return;
-    }
-    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
-      << Attr.getName() << ExpectedVariableOrFunction;
-    return;
-  }
-
-  NamedDecl *nd = cast<NamedDecl>(D);
-
-  nd->addAttr(::new (S.Context)
-              WeakAttr(Attr.getRange(), S.Context,
-                       Attr.getAttributeSpellingListIndex()));
-}
-
 static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
   // weak_import only applies to variable & function declarations.
   bool isDef = false;
@@ -4168,7 +4150,8 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
     handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
   case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
     break;
-  case AttributeList::AT_Weak:        handleWeakAttr        (S, D, Attr); break;
+  case AttributeList::AT_Weak:
+    handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
   case AttributeList::AT_WeakRef:     handleWeakRefAttr     (S, D, Attr); break;
   case AttributeList::AT_WeakImport:  handleWeakImportAttr  (S, D, Attr); break;
   case AttributeList::AT_TransparentUnion:
index 8939a28d75e1aa5162cd5a54bfdd52e2f8da8b4d..2000e7fc683b3ba6232b980b780b06bfdda43342 100644 (file)
@@ -3,7 +3,7 @@
 static int test0 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
 static void test1() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
 
-namespace test2 __attribute__((weak)) { // expected-warning {{'weak' attribute only applies to variables and functions}}
+namespace test2 __attribute__((weak)) { // expected-warning {{'weak' attribute only applies to variables, functions and classes}}
 }
 
 namespace {
@@ -34,3 +34,5 @@ int Test7<T>::var;
 namespace { class Internal; }
 template struct Test7<Internal>;
 template struct Test7<int>;
+
+class __attribute__((weak)) Test8 {}; // OK
index 08bcb501a8de5c7f09aadec2d5335543f5cd825b..d4590597452d83c807a0c1086314edb4ccabba13 100644 (file)
@@ -1797,6 +1797,12 @@ static std::string CalculateDiagnostic(const Record &S) {
     case Func | FuncTemplate:
     case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
     case Func | Var: return "ExpectedVariableOrFunction";
+
+    // If not compiling for C++, the class portion does not apply.
+    case Func | Var | Class:
+      return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
+                                           "ExpectedVariableOrFunction)";
+
     case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
     case Field | Var: return "ExpectedFieldOrGlobalVar";
   }