On Monday 02 Apr 2012 16:20:07 Eli Friedman wrote:
> On Mon, Apr 2, 2012 at 7:01 AM, Tim Northover <[email protected]> wrote:
> > Hi,
> >
> > C90 annoyingly requires a diagnostic to be produced for any type that
> > ends up with two copies of the same type-qualifier. This warning is
> > already produced for cases which can be detected during parsing, but
> > when it happens via a typedef Sema needs to be involved.
> >
> > I believe this patch implements the correct note, though I was uncertain
> > how best to test for C90 only compilation: is !C99 && !C++ & !ObjC
> > correct?
>
> You don't need to check for Objective-C explicitly; it usually just
> follows the rules for the corresponding C version. Otherwise, the
> check is correct: you just need to check for not-C99 and not-C++.
Thanks. That's worth knowing.
> I don't see the point of the check for restrict; it isn't possible to
> write "restrict" in C89 mode, and the standard doesn't constrain
> __restrict.
I did dither about that, happy to remove the diagnostic in that case.
> Err, nevermind about array types; I expect that this does the right
> thing for such types.
Phew! I was scratching my head trying to work out whether the patch,
understanding of the standard or both were wrong.
I've attached an updated patch with your feedback (and some specific array
tests I was fiddling with, just in case).
Could you commit it if it's OK?
Tim.
>From 1594c670a662c1398029120d52aec1aef3cddd16 Mon Sep 17 00:00:00 2001
From: Tim Northover <[email protected]>
Date: Mon, 2 Apr 2012 12:26:26 +0100
Subject: [PATCH] Diagnose duplicate type-qualifiers in pedantic C90 mode
---
lib/Sema/SemaType.cpp | 20 ++++++++++++++++++++
test/Sema/c89.c | 17 +++++++++++++++++
2 files changed, 37 insertions(+)
diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp
index e83e9ef..fbf0cb8 100644
--- a/lib/Sema/SemaType.cpp
+++ b/lib/Sema/SemaType.cpp
@@ -26,6 +26,7 @@
#include "clang/Basic/PartialDiagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
+#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Sema/DeclSpec.h"
#include "clang/Sema/DelayedDiagnostic.h"
#include "clang/Sema/Lookup.h"
@@ -973,6 +974,25 @@ static QualType ConvertDeclSpecToType(TypeProcessingState &state) {
TypeQuals &= ~DeclSpec::TQ_volatile;
}
+ // C90 6.5.3 constraints: "The same type qualifier shall not appear more
+ // than once in the same specifier-list or qualifier-list, either directly
+ // or via one or more typedefs."
+ if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
+ && TypeQuals & Result.getCVRQualifiers()) {
+ if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
+ S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
+ << "const";
+ }
+
+ if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
+ S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
+ << "volatile";
+ }
+
+ // C90 doesn't have restrict, so it doesn't force us to produce a warning
+ // in this case.
+ }
+
Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
Result = Context.getQualifiedType(Result, Quals);
}
diff --git a/test/Sema/c89.c b/test/Sema/c89.c
index 25a1048..110d7e1 100644
--- a/test/Sema/c89.c
+++ b/test/Sema/c89.c
@@ -92,4 +92,21 @@ void test16() {
struct x { int x,y[]; }; /* expected-warning {{Flexible array members are a C99-specific feature}} */
+/* Duplicated type-qualifiers aren't allowed by C90 */
+const const int c_i; /* expected-warning {{duplicate 'const' declaration specifier}} */
+typedef volatile int vol_int;
+volatile vol_int volvol_i; /* expected-warning {{duplicate 'volatile' declaration specifier}} */
+typedef volatile vol_int volvol_int; /* expected-warning {{duplicate 'volatile' declaration specifier}} */
+const int * const c;
+
+typedef const int CI;
+
+const CI mine1[5][5]; /* expected-warning {{duplicate 'const' declaration specifier}} */
+
+typedef CI array_of_CI[5];
+const array_of_CI mine2; /* expected-warning {{duplicate 'const' declaration specifier}} */
+
+typedef CI *array_of_pointer_to_CI[5];
+const array_of_pointer_to_CI mine3;
+
void main() {} /* expected-error {{'main' must return 'int'}} */
--
1.7.9.5
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits