[cfe-commits] r65460 - in /cfe/trunk: Driver/RewriteObjC.cpp include/clang/AST/Decl.h lib/AST/Decl.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaExprCXX.cpp test/Sema/arg-duplicate.c test/Sema/knr-def-call.c

Douglas Gregor dgregor at apple.com
Wed Feb 25 08:33:19 PST 2009


Author: dgregor
Date: Wed Feb 25 10:33:18 2009
New Revision: 65460

URL: http://llvm.org/viewvc/llvm-project?rev=65460&view=rev
Log:
C99 DR #316 implies that the function parameter types that are known
only from a function definition (that does not have a prototype) are
only used to determine the compatible with other declarations of that
same function. In particular, when referencing the function we pretend
as if it does not have a prototype. Implement this behavior, which
fixes PR3626.



Added:
    cfe/trunk/test/Sema/knr-def-call.c
Modified:
    cfe/trunk/Driver/RewriteObjC.cpp
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaExprCXX.cpp
    cfe/trunk/test/Sema/arg-duplicate.c

Modified: cfe/trunk/Driver/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteObjC.cpp?rev=65460&r1=65459&r2=65460&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteObjC.cpp (original)
+++ cfe/trunk/Driver/RewriteObjC.cpp Wed Feb 25 10:33:18 2009
@@ -4063,7 +4063,8 @@
   IdentifierInfo *ID = &Context->Idents.get(name);
   QualType FType = Context->getFunctionTypeNoProto(Context->VoidPtrTy);
   return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), 
-                              ID, FType, FunctionDecl::Extern, false);
+                              ID, FType, FunctionDecl::Extern, false,
+                              false);
 }
 
 Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) {

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=65460&r1=65459&r2=65460&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Wed Feb 25 10:33:18 2009
@@ -532,6 +532,7 @@
   bool IsVirtual : 1;
   bool IsPure : 1;
   bool InheritedPrototype : 1;
+  bool HasPrototype : 1;
   bool IsDeleted : 1;
 
   // Move to DeclGroup when it is implemented.
@@ -545,7 +546,8 @@
       DeclContext(DK),
       ParamInfo(0), Body(0), PreviousDeclaration(0),
       SClass(S), IsInline(isInline), IsVirtual(false), IsPure(false),
-      InheritedPrototype(false), IsDeleted(false), TypeSpecStartLoc(TSSL) {}
+      InheritedPrototype(false), HasPrototype(true), IsDeleted(false), 
+      TypeSpecStartLoc(TSSL) {}
 
   virtual ~FunctionDecl() {}
   virtual void Destroy(ASTContext& C);
@@ -554,6 +556,7 @@
   static FunctionDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
                               DeclarationName N, QualType T, 
                               StorageClass S = None, bool isInline = false,
+                              bool hasPrototype = true,
                               SourceLocation TSStartLoc = SourceLocation());  
   
   SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; }
@@ -588,9 +591,15 @@
   bool isPure() { return IsPure; }
   void setPure() { IsPure = true; }
 
+  /// \brief Whether this function has a prototype, either because one
+  /// was explicitly written or because it was "inherited" by merging
+  /// a declaration without a prototype with a declaration that has a
+  /// prototype.
+  bool hasPrototype() const { return HasPrototype || InheritedPrototype; }
+
   /// \brief Whether this function inherited its prototype from a
   /// previous declaration.
-  bool inheritedPrototype() { return InheritedPrototype; }
+  bool inheritedPrototype() const { return InheritedPrototype; }
   void setInheritedPrototype() { InheritedPrototype = true; }
 
   /// \brief Whether this function has been deleted.

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=65460&r1=65459&r2=65460&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Wed Feb 25 10:33:18 2009
@@ -75,9 +75,13 @@
                                    SourceLocation L, 
                                    DeclarationName N, QualType T, 
                                    StorageClass S, bool isInline, 
+                                   bool hasPrototype,
                                    SourceLocation TypeSpecStartLoc) {
-  return new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
-                                TypeSpecStartLoc);
+  FunctionDecl *New 
+    = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline, 
+                           TypeSpecStartLoc);
+  New->HasPrototype = hasPrototype;
+  return New;
 }
 
 BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=65460&r1=65459&r2=65460&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Feb 25 10:33:18 2009
@@ -334,7 +334,8 @@
   FunctionDecl *New = FunctionDecl::Create(Context,
                                            Context.getTranslationUnitDecl(),
                                            Loc, II, R,
-                                           FunctionDecl::Extern, false);
+                                           FunctionDecl::Extern, false,
+                                           /*hasPrototype=*/true);
   New->setImplicit();
 
   // Create Decl objects for each parameter, adding them to the
@@ -1736,6 +1737,7 @@
       // code path.
       NewFD = FunctionDecl::Create(Context, DC, D.getIdentifierLoc(),
                                    Name, R, SC, isInline, 
+                                   /*hasPrototype=*/true,
                                    // FIXME: Move to DeclGroup...
                                    D.getDeclSpec().getSourceRange().getBegin());
       InvalidDecl = true;
@@ -1765,6 +1767,10 @@
     NewFD = FunctionDecl::Create(Context, DC,
                                  D.getIdentifierLoc(),
                                  Name, R, SC, isInline, 
+                                 /*hasPrototype=*/
+                                   (getLangOptions().CPlusPlus ||
+                                    (D.getNumTypeObjects() &&
+                                     D.getTypeObject(0).Fun.hasPrototype)),
                                  // FIXME: Move to DeclGroup...
                                  D.getDeclSpec().getSourceRange().getBegin());
   }

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=65460&r1=65459&r2=65460&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Feb 25 10:33:18 2009
@@ -873,6 +873,19 @@
           CheckS = CheckS->getParent();
       }
     }
+  } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(VD)) {
+    if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) {
+      // C99 DR 316 says that, if a function type comes from a
+      // function definition (without a prototype), that type is only
+      // used for checking compatibility. Therefore, when referencing
+      // the function, we pretend that we don't have the full function
+      // type.
+      QualType T = Func->getType();
+      QualType NoProtoType = T;
+      if (const FunctionTypeProto *Proto = T->getAsFunctionTypeProto())
+        NoProtoType = Context.getFunctionTypeNoProto(Proto->getResultType());
+      return Owned(BuildDeclRefExpr(VD, NoProtoType, Loc, false, false, SS));
+    }
   }
 
   // Only create DeclRefExpr's for valid Decl's.

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=65460&r1=65459&r2=65460&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Wed Feb 25 10:33:18 2009
@@ -543,7 +543,7 @@
   QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0);
   FunctionDecl *Alloc =
     FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name,
-                         FnType, FunctionDecl::None, false,
+                         FnType, FunctionDecl::None, false, true,
                          SourceLocation());
   Alloc->setImplicit();
   ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),

Modified: cfe/trunk/test/Sema/arg-duplicate.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/arg-duplicate.c?rev=65460&r1=65459&r2=65460&view=diff

==============================================================================
--- cfe/trunk/test/Sema/arg-duplicate.c (original)
+++ cfe/trunk/test/Sema/arg-duplicate.c Wed Feb 25 10:33:18 2009
@@ -9,6 +9,6 @@
 } 
 
 void f4(void) { 
-  f3 (1, 1, 2, 3, 4);   // expected-error {{too many arguments to function}}
+  f3 (1, 1, 2, 3, 4);
 }
 

Added: cfe/trunk/test/Sema/knr-def-call.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/knr-def-call.c?rev=65460&view=auto

==============================================================================
--- cfe/trunk/test/Sema/knr-def-call.c (added)
+++ cfe/trunk/test/Sema/knr-def-call.c Wed Feb 25 10:33:18 2009
@@ -0,0 +1,14 @@
+// RUN: clang -fsyntax-only -verify %s
+
+// C DR #316, PR 3626.
+void f0(a, b, c, d) int a,b,c,d; {}
+void t0(void) { f0(1); }
+
+void f1(a, b) int a, b; {}
+void t1(void) { f1(1, 2, 3); }
+
+void f2(float); // expected-note{{previous declaration is here}}
+void f2(x) float x; { } // expected-error{{conflicting types for 'f2'}}
+
+typedef void (*f3)(void);
+f3 t3(int b) { return b? f0 : f1; } // okay





More information about the cfe-commits mailing list