[cfe-commits] r79070 - in /cfe/trunk: include/clang/AST/Decl.h lib/AST/Decl.cpp lib/CodeGen/Mangle.cpp lib/Sema/SemaDecl.cpp test/Sema/freemain.c

John McCall rjmccall at apple.com
Fri Aug 14 19:09:25 PDT 2009


Author: rjmccall
Date: Fri Aug 14 21:09:25 2009
New Revision: 79070

URL: http://llvm.org/viewvc/llvm-project?rev=79070&view=rev
Log:
Disable all recognition of main() in -ffreestanding.  Addresses bug #4720.


Added:
    cfe/trunk/test/Sema/freemain.c
Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/CodeGen/Mangle.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Fri Aug 14 21:09:25 2009
@@ -850,7 +850,7 @@
 
   /// \brief Determines whether this is a function "main", which is
   /// the entry point into an executable program.
-  bool isMain() const;
+  bool isMain(ASTContext &Context) const;
 
   /// \brief Determines whether this function is a function with
   /// external, C linkage.

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

==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Fri Aug 14 21:09:25 2009
@@ -414,8 +414,9 @@
     EndRangeLoc = B->getLocEnd();
 }
 
-bool FunctionDecl::isMain() const {
-  return getDeclContext()->getLookupContext()->isTranslationUnit() &&
+bool FunctionDecl::isMain(ASTContext &Context) const {
+  return !Context.getLangOptions().Freestanding &&
+    getDeclContext()->getLookupContext()->isTranslationUnit() &&
     getIdentifier() && getIdentifier()->isStr("main");
 }
 

Modified: cfe/trunk/lib/CodeGen/Mangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/Mangle.cpp?rev=79070&r1=79069&r2=79070&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.cpp (original)
+++ cfe/trunk/lib/CodeGen/Mangle.cpp Fri Aug 14 21:09:25 2009
@@ -91,7 +91,7 @@
   // name mangling (always).
   if (!FD->hasAttr<OverloadableAttr>()) {
     // C functions are not mangled, and "main" is never mangled.
-    if (!Context.getLangOptions().CPlusPlus || FD->isMain())
+    if (!Context.getLangOptions().CPlusPlus || FD->isMain(Context))
       return false;
     
     // No mangling in an "implicit extern C" header. 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Aug 14 21:09:25 2009
@@ -2766,7 +2766,7 @@
     return NewFD->setInvalidDecl();
   }
 
-  if (NewFD->isMain()) CheckMain(NewFD);
+  if (NewFD->isMain(Context)) CheckMain(NewFD);
 
   // Semantic checking for this function declaration (in isolation).
   if (getLangOptions().CPlusPlus) {
@@ -3542,7 +3542,7 @@
   //   definition itself provides a prototype. The aim is to detect
   //   global functions that fail to be declared in header files.
   if (!FD->isInvalidDecl() && FD->isGlobal() && !isa<CXXMethodDecl>(FD) &&
-      !FD->isMain()) {
+      !FD->isMain(Context)) {
     bool MissingPrototype = true;
     for (const FunctionDecl *Prev = FD->getPreviousDeclaration();
          Prev; Prev = Prev->getPreviousDeclaration()) {
@@ -3608,7 +3608,7 @@
   Stmt *Body = BodyArg.takeAs<Stmt>();
   if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(dcl)) {
     FD->setBody(Body);
-    if (FD->isMain())
+    if (FD->isMain(Context))
       // C and C++ allow for main to automagically return 0.
       // Implements C++ [basic.start.main]p5 and C99 5.1.2.2.3.
       FD->setHasImplicitReturnZero(true);

Added: cfe/trunk/test/Sema/freemain.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/freemain.c?rev=79070&view=auto

==============================================================================
--- cfe/trunk/test/Sema/freemain.c (added)
+++ cfe/trunk/test/Sema/freemain.c Fri Aug 14 21:09:25 2009
@@ -0,0 +1,9 @@
+// RUN: clang-cc -fsyntax-only -verify -ffreestanding %s
+
+// Tests that -ffreestanding disables all special treatment of main().
+
+void* allocate(long size);
+
+void* main(void* context, long size) {
+  if (context) return allocate(size);
+} // expected-warning {{control may reach end of non-void function}}





More information about the cfe-commits mailing list