e.g.
void g() __attribute__((deprecated));
void g();
int a() {
//should produce a warning. it does not as clang discards the old
defnition and its attributes when merging
g();
}
Ok, for now, I just disabled these cases from the test case. It would
be nice to have this but can happen as a separate step.
Ok. But how should clang handle this attribute merging? Simply append
the old declaration atrributes to the new ones? Append just
non-repeating ones? Or..?
It depends on the attributes, but yes they should generally be merged.
fixed in the patch in the easiest way.
BTW, I just saw how noreturn is handled (it enforces 0 parameters). Do
you prefer having this enforcement done in deprecated as well as other
attributes?
Yep, please.
Ok, done in the patch.
In the patch there's also code to handle nothrow+noreturn (including
codegen), partial format support (needs tweaking in the callexpr sema), and
adds a few more attributes that aren't used yet.
Anyway, enough for today!
Nuno
Index: test/CodeGen/noreturn.c
===================================================================
--- test/CodeGen/noreturn.c (revision 0)
+++ test/CodeGen/noreturn.c (revision 0)
@@ -0,0 +1,4 @@
+// RUN: clang -emit-llvm < %s | grep noreturn
+
+void f() __attribute__((noreturn));
+void f() {}
Index: test/CodeGen/nothrow.c
===================================================================
--- test/CodeGen/nothrow.c (revision 0)
+++ test/CodeGen/nothrow.c (revision 0)
@@ -0,0 +1,4 @@
+// RUN: clang -emit-llvm < %s | grep nounwind
+
+void f() __attribute__((nothrow));
+void f() {}
Index: test/CodeGen/weak-function.c
===================================================================
--- test/CodeGen/weak-function.c (revision 0)
+++ test/CodeGen/weak-function.c (revision 0)
@@ -0,0 +1,4 @@
+// RUN: clang -emit-llvm < %s | grep weak
+
+void f() __attribute__((weak));
+void f() {}
Index: test/CodeGen/weak-var.c
===================================================================
--- test/CodeGen/weak-var.c (revision 0)
+++ test/CodeGen/weak-var.c (revision 0)
@@ -0,0 +1,3 @@
+// RUN: clang -emit-llvm < %s | grep weak
+
+int i __attribute__((weak)) = 2;
Index: test/Sema/deprecated.c
===================================================================
--- test/Sema/deprecated.c (revision 47778)
+++ test/Sema/deprecated.c (working copy)
@@ -13,7 +13,7 @@
f(); // expected-warning {{'f' is deprecated}}
// test if attributes propagate to functions
- g(); // todo-warning {{'g' is deprecated}}
+ g(); // expected-warning {{'g' is deprecated}}
return var; // expected-warning {{'var' is deprecated}}
}
@@ -21,5 +21,5 @@
// test if attributes propagate to variables
extern int var;
int w() {
- return var; // todo-warning {{'var' is deprecated}}
+ return var; // expected-warning {{'var' is deprecated}}
}
Index: include/clang/Basic/DiagnosticKinds.def
===================================================================
--- include/clang/Basic/DiagnosticKinds.def (revision 47778)
+++ include/clang/Basic/DiagnosticKinds.def (working copy)
@@ -567,6 +567,14 @@
"invalid vector type '%0'")
DIAG(err_attribute_argument_not_int, ERROR,
"'%0' attribute requires integer constant")
+DIAG(err_attribute_argument_n_not_int, ERROR,
+ "'%0' attribute requires parameter %1 to be an integer constant")
+DIAG(err_attribute_argument_n_not_string, ERROR,
+ "'%0' attribute requires parameter %1 to be a string")
+DIAG(err_attribute_argument_out_of_bounds, ERROR,
+ "'%0' attribute parameter %1 is out of bounds")
+DIAG(err_format_strftime_third_parameter, ERROR,
+ "strftime format attribute requires 3rd parameter to be 0")
DIAG(err_attribute_invalid_size, ERROR,
"vector size not an integral multiple of component size")
DIAG(err_attribute_zero_size, ERROR,
@@ -591,6 +599,8 @@
"'%0' attribute ignored")
DIAG(warn_attribute_ignored_for_field_of_type, WARNING,
"'%0' attribute ignored for field of type '%1'")
+DIAG(warn_attribute_type_not_supported, WARNING,
+ "'%0' attribute argument not supported: '%1'")
// Function Parameter Semantic Analysis.
DIAG(err_param_with_void_type, ERROR,
Index: include/clang/AST/Attr.h
===================================================================
--- include/clang/AST/Attr.h (revision 47778)
+++ include/clang/AST/Attr.h (working copy)
@@ -27,7 +27,10 @@
Packed,
Annotate,
NoReturn,
- Deprecated
+ Deprecated,
+ Weak,
+ Nothrow,
+ Format
};
private:
@@ -118,6 +121,42 @@
static bool classof(const DeprecatedAttr *A) { return true; }
};
+class WeakAttr : public Attr {
+public:
+ WeakAttr() : Attr(Weak) {}
+
+ // Implement isa/cast/dyncast/etc.
+
+ static bool classof(const Attr *A) { return A->getKind() == Weak; }
+ static bool classof(const WeakAttr *A) { return true; }
+};
+
+class NothrowAttr : public Attr {
+public:
+ NothrowAttr() : Attr(Nothrow) {}
+
+ // Implement isa/cast/dyncast/etc.
+
+ static bool classof(const Attr *A) { return A->getKind() == Nothrow; }
+ static bool classof(const NothrowAttr *A) { return true; }
+};
+
+class FormatAttr : public Attr {
+ std::string Type;
+ int formatIdx, firstArg;
+public:
+ FormatAttr(const std::string &type, int idx, int first) : Attr(Format),
Type(type), formatIdx(idx), firstArg(first) {}
+
+ const std::string& getType() const { return Type; }
+ int getFormatIdx() const { return formatIdx; }
+ int getFirstArg() const { return firstArg; }
+
+ // Implement isa/cast/dyncast/etc.
+
+ static bool classof(const Attr *A) { return A->getKind() == Format; }
+ static bool classof(const FormatAttr *A) { return true; }
+};
+
} // end namespace clang
#endif
Index: include/clang/Parse/AttributeList.h
===================================================================
--- include/clang/Parse/AttributeList.h (revision 47778)
+++ include/clang/Parse/AttributeList.h (working copy)
@@ -50,7 +50,16 @@
AT_packed,
AT_annotate,
AT_noreturn,
- AT_deprecated
+ AT_deprecated,
+ AT_unused,
+ AT_format,
+ AT_nonnull,
+ AT_malloc,
+ AT_pure,
+ AT_weak,
+ AT_nothrow,
+ AT_noinline,
+ AT_warn_unused_result
};
IdentifierInfo *getName() const { return AttrName; }
Index: Sema/Sema.h
===================================================================
--- Sema/Sema.h (revision 47778)
+++ Sema/Sema.h (working copy)
@@ -273,6 +273,10 @@
void HandlePackedAttribute(Decl *d, AttributeList *rawAttr);
void HandleAnnotateAttribute(Decl *d, AttributeList *rawAttr);
void HandleNoReturnAttribute(Decl *d, AttributeList *rawAttr);
+ void HandleDeprecatedAttribute(Decl *d, AttributeList *rawAttr);
+ void HandleWeakAttribute(Decl *d, AttributeList *rawAttr);
+ void HandleNothrowAttribute(Decl *d, AttributeList *rawAttr);
+ void HandleFormatAttribute(Decl *d, AttributeList *rawAttr);
void WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
bool &IncompleteImpl);
Index: Sema/SemaDecl.cpp
===================================================================
--- Sema/SemaDecl.cpp (revision 47778)
+++ Sema/SemaDecl.cpp (working copy)
@@ -242,6 +242,24 @@
return New;
}
+/// DeclHasAttr - returns true if decl Declaration already has the target
attribute
+static bool DeclHasAttr(const Decl *decl, const Attr *target) {
+ for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
+ if (attr->getKind() == target->getKind())
+ return true;
+
+ return false;
+}
+
+/// MergeAttributes - append attributes from the Old decl to the New one
+static void MergeAttributes(Decl *New, Decl *Old) {
+ for (const Attr *attr = Old->getAttrs(); attr; attr = attr->getNext()) {
+ if (!DeclHasAttr(New, attr)) {
+ New->addAttr(const_cast<Attr*>(attr));
+ }
+ }
+}
+
/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
/// and scope as a previous declaration 'Old'. Figure out how to resolve this
/// situation, merging decls or emitting diagnostics as appropriate.
@@ -256,7 +274,8 @@
return New;
}
- // FIXME: propagate old Attrs to the New decl
+ MergeAttributes(New, Old);
+
QualType OldQType = Old->getCanonicalType();
QualType NewQType = New->getCanonicalType();
@@ -326,6 +345,9 @@
Diag(OldD->getLocation(), diag::err_previous_definition);
return New;
}
+
+ MergeAttributes(New, Old);
+
// Verify the types match.
if (Old->getCanonicalType() != New->getCanonicalType() &&
!areEquivalentArrayTypes(New->getCanonicalType(), Old->getCanonicalType())) {
@@ -1781,8 +1803,14 @@
}
break;
case AttributeList::AT_deprecated:
- New->addAttr(new DeprecatedAttr());
+ HandleDeprecatedAttribute(New, Attr);
break;
+ case AttributeList::AT_weak:
+ HandleWeakAttribute(New, Attr);
+ break;
+ case AttributeList::AT_nothrow:
+ HandleNothrowAttribute(New, Attr);
+ break;
case AttributeList::AT_aligned:
HandleAlignedAttribute(New, Attr);
break;
@@ -1795,6 +1823,9 @@
case AttributeList::AT_noreturn:
HandleNoReturnAttribute(New, Attr);
break;
+ case AttributeList::AT_format:
+ HandleFormatAttribute(New, Attr);
+ break;
default:
#if 0
// TODO: when we have the full set of attributes, warn about unknown ones.
@@ -1952,6 +1983,119 @@
d->addAttr(new NoReturnAttr());
}
+void Sema::HandleDeprecatedAttribute(Decl *d, AttributeList *rawAttr) {
+ // check the attribute arguments.
+ if (rawAttr->getNumArgs() != 0) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
+ std::string("0"));
+ return;
+ }
+
+ d->addAttr(new DeprecatedAttr());
+}
+
+void Sema::HandleWeakAttribute(Decl *d, AttributeList *rawAttr) {
+ // check the attribute arguments.
+ if (rawAttr->getNumArgs() != 0) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
+ std::string("0"));
+ return;
+ }
+
+ d->addAttr(new WeakAttr());
+}
+
+void Sema::HandleNothrowAttribute(Decl *d, AttributeList *rawAttr) {
+ // check the attribute arguments.
+ if (rawAttr->getNumArgs() != 0) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
+ std::string("0"));
+ return;
+ }
+
+ d->addAttr(new NothrowAttr());
+}
+
+void Sema::HandleFormatAttribute(Decl *d, AttributeList *rawAttr) {
+
+ if (!rawAttr->getParameterName()) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string,
+ "format", std::string("1"));
+ return;
+ }
+
+ if (rawAttr->getNumArgs() != 2) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
+ std::string("3"));
+ return;
+ }
+
+ FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
+ if (!Fn) {
+ return;
+ }
+
+ // FIXME: in C++ the implicit 'this' function parameter also counts.
+ // the index must start in 1 and the limit is numargs+1
+ unsigned NumArgs = Fn->getNumParams()+1; // +1 for ...
+
+ const char *Format = rawAttr->getParameterName()->getName();
+ unsigned FormatLen = rawAttr->getParameterName()->getLength();
+
+ // Normalize the argument, __foo__ becomes foo.
+ if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
+ Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
+ Format += 2;
+ FormatLen -= 4;
+ }
+
+ if (!((FormatLen == 5 && !memcmp(Format, "scanf", 5))
+ || (FormatLen == 6 && !memcmp(Format, "printf", 6))
+ || (FormatLen == 7 && !memcmp(Format, "strfmon", 7))
+ || (FormatLen == 8 && !memcmp(Format, "strftime", 8)))) {
+ Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported,
+ "format", rawAttr->getParameterName()->getName());
+ return;
+ }
+
+ Expr *IdxExpr = static_cast<Expr *>(rawAttr->getArg(0));
+ llvm::APSInt Idx(32);
+ if (!IdxExpr->isIntegerConstantExpr(Idx, Context)) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int,
+ "format", std::string("2"), IdxExpr->getSourceRange());
+ return;
+ }
+
+ if (Idx.getZExtValue() < 1 || Idx.getZExtValue() > NumArgs) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds,
+ "format", std::string("2"), IdxExpr->getSourceRange());
+ return;
+ }
+
+ Expr *FirstArgExpr = static_cast<Expr *>(rawAttr->getArg(1));
+ llvm::APSInt FirstArg(32);
+ if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, Context)) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int,
+ "format", std::string("3"), FirstArgExpr->getSourceRange());
+ return;
+ }
+
+ if (FormatLen == 8 && !memcmp(Format, "strftime", 8)) {
+ if (FirstArg.getZExtValue() != 0) {
+ Diag(rawAttr->getLoc(), diag::err_format_strftime_third_parameter,
+ FirstArgExpr->getSourceRange());
+ return;
+ }
+ } else if (FirstArg.getZExtValue() > NumArgs) {
+ Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds,
+ "format", std::string("3"), FirstArgExpr->getSourceRange());
+ return;
+ }
+
+ d->addAttr(new FormatAttr(std::string(Format, FormatLen),
+ Idx.getZExtValue(), FirstArg.getZExtValue()));
+}
+
void Sema::HandleAnnotateAttribute(Decl *d, AttributeList *rawAttr) {
// check the attribute arguments.
if (rawAttr->getNumArgs() != 1) {
Index: Driver/clang.cpp
===================================================================
--- Driver/clang.cpp (revision 47778)
+++ Driver/clang.cpp (working copy)
@@ -865,6 +865,7 @@
}
AddPath("/usr/local/include", System, false, false, false, Headers);
+ AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include", System, false,
false, false, Headers); // nuno's laptop
// leopard
AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System,
false, false, false, Headers);
Index: CodeGen/CodeGenModule.cpp
===================================================================
--- CodeGen/CodeGenModule.cpp (revision 47778)
+++ CodeGen/CodeGenModule.cpp (working copy)
@@ -237,23 +237,28 @@
GV->setInitializer(Init);
// Set the llvm linkage type as appropriate.
- // FIXME: This isn't right. This should handle common linkage and other
- // stuff.
- switch (D->getStorageClass()) {
- case VarDecl::Auto:
- case VarDecl::Register:
- assert(0 && "Can't have auto or register globals");
- case VarDecl::None:
- if (!D->getInit())
- GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
- break;
- case VarDecl::Extern:
- case VarDecl::PrivateExtern:
- // todo: common
- break;
- case VarDecl::Static:
- GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
- break;
+ if (D->getAttr<WeakAttr>()) {
+ GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
+
+ } else {
+ // FIXME: This isn't right. This should handle common linkage and other
+ // stuff.
+ switch (D->getStorageClass()) {
+ case VarDecl::Auto:
+ case VarDecl::Register:
+ assert(0 && "Can't have auto or register globals");
+ case VarDecl::None:
+ if (!D->getInit())
+ GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
+ break;
+ case VarDecl::Extern:
+ case VarDecl::PrivateExtern:
+ // todo: common
+ break;
+ case VarDecl::Static:
+ GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
+ break;
+ }
}
}
Index: CodeGen/CodeGenFunction.cpp
===================================================================
--- CodeGen/CodeGenFunction.cpp (revision 47778)
+++ CodeGen/CodeGenFunction.cpp (working copy)
@@ -18,6 +18,7 @@
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
+#include "llvm/ParamAttrsList.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Support/CFG.h"
using namespace clang;
@@ -67,11 +68,25 @@
// TODO: Set up linkage and many other things. Note, this is a simple
// approximation of what we really want.
- if (FD->getStorageClass() == FunctionDecl::Static)
+ if (FD->getAttr<WeakAttr>() || FD->isInline())
+ CurFn->setLinkage(llvm::Function::WeakLinkage);
+ else if (FD->getStorageClass() == FunctionDecl::Static)
CurFn->setLinkage(llvm::Function::InternalLinkage);
- else if (FD->isInline())
- CurFn->setLinkage(llvm::Function::WeakLinkage);
-
+
+
+ llvm::ParamAttrsVector ParamAttrsVec;
+
+ if (FD->getAttr<NothrowAttr>())
+ ParamAttrsVec.push_back(
+ llvm::ParamAttrsWithIndex::get(ParamAttrsVec.size(),
llvm::ParamAttr::NoUnwind));
+ if (FD->getAttr<NoReturnAttr>())
+ ParamAttrsVec.push_back(
+ llvm::ParamAttrsWithIndex::get(ParamAttrsVec.size(),
llvm::ParamAttr::NoReturn));
+
+ if (!ParamAttrsVec.empty())
+ CurFn->setParamAttrs(llvm::ParamAttrsList::get(ParamAttrsVec));
+
+
llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
// Create a marker to make it easy to insert allocas into the entryblock
Index: Parse/AttributeList.cpp
===================================================================
--- Parse/AttributeList.cpp (revision 47778)
+++ Parse/AttributeList.cpp (working copy)
@@ -51,15 +51,25 @@
}
switch (Len) {
- case 6:
+ case 4:
+ if (!memcmp(Str, "weak", 4)) return AT_weak;
+ if (!memcmp(Str, "pure", 4)) return AT_pure;
+ break;
+ case 6:
if (!memcmp(Str, "packed", 6)) return AT_packed;
+ if (!memcmp(Str, "malloc", 6)) return AT_malloc;
+ if (!memcmp(Str, "format", 6)) return AT_format;
+ if (!memcmp(Str, "unused", 6)) return AT_unused;
break;
case 7:
if (!memcmp(Str, "aligned", 7)) return AT_aligned;
+ if (!memcmp(Str, "nothrow", 7)) return AT_nothrow;
+ if (!memcmp(Str, "nonnull", 7)) return AT_nonnull;
break;
case 8:
if (!memcmp(Str, "annotate", 8)) return AT_annotate;
if (!memcmp(Str, "noreturn", 8)) return AT_noreturn;
+ if (!memcmp(Str, "noinline", 8)) return AT_noinline;
break;
case 10:
if (!memcmp(Str, "deprecated", 10)) return AT_deprecated;
@@ -73,6 +83,9 @@
case 15:
if (!memcmp(Str, "ocu_vector_type", 15)) return AT_ocu_vector_type;
break;
- }
+ case 18:
+ if (!memcmp(Str, "warn_unused_result", 18)) return AT_warn_unused_result;
+ break;
+ }
return UnknownAttribute;
}
_______________________________________________
cfe-dev mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev