[cfe-commits] r90239 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDeclCXX.cpp lib/Sema/SemaExpr.cpp test/SemaCXX/attr-cxx0x.cpp test/SemaTemplate/class-template-id.cpp
Douglas Gregor
dgregor at apple.com
Tue Dec 1 08:58:18 PST 2009
Author: dgregor
Date: Tue Dec 1 10:58:18 2009
New Revision: 90239
URL: http://llvm.org/viewvc/llvm-project?rev=90239&view=rev
Log:
Don't automatically assume that an id-expression refers to a
ValueDecl, because that isn't always the case in ill-formed
code. Diagnose a common mistake (forgetting to provide a template
argument list for a class template, PR5655) and dyn_cast so that we
handle the general problem of referring to a non-value declaration
gracefully.
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaDeclCXX.cpp
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/SemaCXX/attr-cxx0x.cpp
cfe/trunk/test/SemaTemplate/class-template-id.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=90239&r1=90238&r2=90239&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Dec 1 10:58:18 2009
@@ -441,7 +441,7 @@
"of type %1">;
def note_field_decl : Note<"member is declared here">;
-def note_previous_class_decl : Note<
+def note_previous_decl : Note<
"%0 declared here">;
def note_member_synthesized_at : Note<
"implicit default %select{constructor|copy constructor|"
@@ -934,6 +934,8 @@
"extraneous 'template<>' in declaration of variable %0">;
def err_template_tag_noparams : Error<
"extraneous 'template<>' in declaration of %0 %1">;
+def err_template_decl_ref : Error<
+ "cannot refer to class template %0 without a template argument list">;
// C++ Template Argument Lists
def err_template_arg_list_different_arity : Error<
@@ -1649,6 +1651,7 @@
def err_unexpected_interface : Error<
"unexpected interface name %0: expected expression">;
+def err_ref_non_value : Error<"%0 does not refer to a value">;
def err_property_not_found : Error<
"property %0 not found on object of type %1">;
def ext_gnu_void_ptr : Extension<
Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=90239&r1=90238&r2=90239&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue Dec 1 10:58:18 2009
@@ -489,8 +489,8 @@
// C++0x CWG Issue #817 indicates that [[final]] classes shouldn't be bases.
if (CXXBaseDecl->hasAttr<FinalAttr>()) {
Diag(BaseLoc, diag::err_final_base) << BaseType.getAsString();
- Diag(CXXBaseDecl->getLocation(), diag::note_previous_class_decl)
- << BaseType.getAsString();
+ Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl)
+ << BaseType;
return 0;
}
@@ -1284,7 +1284,7 @@
Diag(Constructor->getLocation(), diag::err_missing_default_ctor)
<< (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl)
<< 0 << VBase->getType();
- Diag(VBaseDecl->getLocation(), diag::note_previous_class_decl)
+ Diag(VBaseDecl->getLocation(), diag::note_previous_decl)
<< Context.getTagDeclType(VBaseDecl);
HadError = true;
continue;
@@ -1332,7 +1332,7 @@
Diag(Constructor->getLocation(), diag::err_missing_default_ctor)
<< (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl)
<< 0 << Base->getType();
- Diag(BaseDecl->getLocation(), diag::note_previous_class_decl)
+ Diag(BaseDecl->getLocation(), diag::note_previous_decl)
<< Context.getTagDeclType(BaseDecl);
HadError = true;
continue;
@@ -1399,7 +1399,7 @@
<< (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl)
<< 1 << (*Field)->getDeclName();
Diag(Field->getLocation(), diag::note_field_decl);
- Diag(RT->getDecl()->getLocation(), diag::note_previous_class_decl)
+ Diag(RT->getDecl()->getLocation(), diag::note_previous_decl)
<< Context.getTagDeclType(RT->getDecl());
HadError = true;
continue;
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=90239&r1=90238&r2=90239&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Dec 1 10:58:18 2009
@@ -1246,7 +1246,23 @@
if (CheckDeclInExpr(*this, Loc, D))
return ExprError();
- ValueDecl *VD = cast<ValueDecl>(D);
+ if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
+ // Specifically diagnose references to class templates that are missing
+ // a template argument list.
+ Diag(Loc, diag::err_template_decl_ref)
+ << Template << SS.getRange();
+ Diag(Template->getLocation(), diag::note_template_decl_here);
+ return ExprError();
+ }
+
+ // Make sure that we're referring to a value.
+ ValueDecl *VD = dyn_cast<ValueDecl>(D);
+ if (!VD) {
+ Diag(Loc, diag::err_ref_non_value)
+ << D << SS.getRange();
+ Diag(D->getLocation(), diag::note_previous_decl);
+ return ExprError();
+ }
// Check whether this declaration can be used. Note that we suppress
// this check when we're going to perform argument-dependent lookup
Modified: cfe/trunk/test/SemaCXX/attr-cxx0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/attr-cxx0x.cpp?rev=90239&r1=90238&r2=90239&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/attr-cxx0x.cpp (original)
+++ cfe/trunk/test/SemaCXX/attr-cxx0x.cpp Tue Dec 1 10:58:18 2009
@@ -2,7 +2,7 @@
int final_fail [[final]]; //expected-error {{'final' attribute only applies to virtual method or class types}}
-struct [[final]] final_base { }; // expected-note {{struct final_base declared here}}
+struct [[final]] final_base { }; // expected-note {{'struct final_base' declared here}}
struct final_child : final_base { }; // expected-error {{derivation from 'final' struct final_base}}
struct final_member { virtual void quux [[final]] (); }; // expected-note {{overridden virtual function is here}}
@@ -33,4 +33,4 @@
};
struct [[base_check, base_check]] bc : base { // expected-error {{'base_check' attribute cannot be repeated}}
-};
\ No newline at end of file
+};
Modified: cfe/trunk/test/SemaTemplate/class-template-id.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/class-template-id.cpp?rev=90239&r1=90238&r2=90239&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/class-template-id.cpp (original)
+++ cfe/trunk/test/SemaTemplate/class-template-id.cpp Tue Dec 1 10:58:18 2009
@@ -36,3 +36,8 @@
N::C<int> c1;
typedef N::C<float> c2;
+
+// PR5655
+template<typename T> struct Foo { }; // expected-note{{template is declared here}}
+
+void f(void) { Foo bar; } // expected-error{{without a template argument list}}
More information about the cfe-commits
mailing list