"classes and virtual methods|functions, methods, and parameters|"
"classes|virtual methods|class members|variables|methods}1">;
def warn_function_attribute_wrong_type : Warning<
- "%0 only applies to function types; type here is %1">;
+ "'%0' only applies to function types; type here is %1">;
+def warn_pointer_attribute_wrong_type : Warning<
+ "'%0' only applies to pointer types; type here is %1">;
def warn_gnu_inline_attribute_requires_inline : Warning<
"'gnu_inline' attribute requires function to be marked 'inline',"
" attribute ignored">;
#include "clang/AST/Expr.h"
#include "clang/Basic/PartialDiagnostic.h"
#include "clang/Basic/TargetInfo.h"
+#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/DeclSpec.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/ErrorHandling.h"
return false;
}
+/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
+/// doesn't apply to the given type.
+static void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
+ QualType type) {
+ bool useInstantiationLoc = false;
+
+ unsigned diagID = 0;
+ switch (attr.getKind()) {
+ case AttributeList::AT_objc_gc:
+ diagID = diag::warn_pointer_attribute_wrong_type;
+ useInstantiationLoc = true;
+ break;
+
+ default:
+ // Assume everything else was a function attribute.
+ diagID = diag::warn_function_attribute_wrong_type;
+ break;
+ }
+
+ SourceLocation loc = attr.getLoc();
+ llvm::StringRef name = attr.getName()->getName();
+
+ // The GC attributes are usually written with macros; special-case them.
+ if (useInstantiationLoc && loc.isMacroID() && attr.getParameterName()) {
+ SourceLocation instLoc = S.getSourceManager().getInstantiationLoc(loc);
+ llvm::StringRef macro = S.getPreprocessor().getSpelling(instLoc);
+ if ((macro == "__strong" && attr.getParameterName()->isStr("strong")) ||
+ (macro == "__weak" && attr.getParameterName()->isStr("weak"))) {
+ loc = instLoc;
+ name = macro;
+ }
+ }
+
+ S.Diag(loc, diagID) << name << type;
+}
+
// objc_gc applies to Objective-C pointers or, otherwise, to the
// smallest available pointer type (i.e. 'void*' in 'void**').
#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
void diagnoseIgnoredTypeAttrs(QualType type) const {
for (llvm::SmallVectorImpl<AttributeList*>::const_iterator
i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
- i != e; ++i) {
- AttributeList &attr = **i;
- getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
- << attr.getName() << type;
- }
+ i != e; ++i)
+ diagnoseBadTypeAttribute(getSema(), **i, type);
}
~TypeProcessingState() {
}
}
error:
-
- state.getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
- << attr.getName() << type;
+
+ diagnoseBadTypeAttribute(state.getSema(), attr, type);
}
/// Distribute an objc_gc type attribute that was written on the
}
}
- state.getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
- << attr.getName() << type;
+ diagnoseBadTypeAttribute(state.getSema(), attr, type);
}
/// Try to distribute a function type attribute to the innermost
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify %s
static id __attribute((objc_gc(weak))) a;
static id __attribute((objc_gc(strong))) b;
static id __attribute((objc_gc(123))) d; // expected-error{{'objc_gc' attribute requires parameter 1 to be a string}}
static id __attribute((objc_gc(foo, 456))) e; // expected-error{{attribute takes one argument}}
static id __attribute((objc_gc(hello))) f; // expected-warning{{'objc_gc' attribute argument not supported: 'hello'}}
+
+static int __attribute__((objc_gc(weak))) g; // expected-warning {{'objc_gc' only applies to pointer types; type here is 'int'}}
+
+static __weak int h; // expected-warning {{'__weak' only applies to pointer types; type here is 'int'}}