r252902 - [C++] Add the "norecurse" attribute to main() if in C++ mode

James Molloy via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 07:36:04 PST 2015


Author: jamesm
Date: Thu Nov 12 09:36:04 2015
New Revision: 252902

URL: http://llvm.org/viewvc/llvm-project?rev=252902&view=rev
Log:
[C++] Add the "norecurse" attribute to main() if in C++ mode

The C++ spec (3.6.1.3) says "The function `main` shall not be used within a program". This implies that it cannot recurse, so add the norecurse attribute to help the midend out a bit.

Added:
    cfe/trunk/test/CodeGenCXX/main-norecurse.cpp
Modified:
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/test/CodeGenObjC/objc-literal-tests.m

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=252902&r1=252901&r2=252902&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Thu Nov 12 09:36:04 2015
@@ -716,6 +716,14 @@ void CodeGenFunction::StartFunction(Glob
     }
   }
 
+  // If we're in C++ mode and the function name is "main", it is guaranteed
+  // to be norecurse by the standard (3.6.1.3 "The function main shall not be
+  // used within a program").
+  if (getLangOpts().CPlusPlus)
+    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
+      if (FD->isMain())
+        Fn->addFnAttr(llvm::Attribute::NoRecurse);
+  
   llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
 
   // Create a marker to make it easy to insert allocas into the entryblock

Added: cfe/trunk/test/CodeGenCXX/main-norecurse.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/main-norecurse.cpp?rev=252902&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/main-norecurse.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/main-norecurse.cpp Thu Nov 12 09:36:04 2015
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s
+
+// CHECK: define i{{.*}} @main({{.*}}) #0
+int main(int argc, char **argv) {
+    return 1;
+}
+
+// CHECK: attributes #0 = { norecurse{{.*}} }

Modified: cfe/trunk/test/CodeGenObjC/objc-literal-tests.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/objc-literal-tests.m?rev=252902&r1=252901&r2=252902&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenObjC/objc-literal-tests.m (original)
+++ cfe/trunk/test/CodeGenObjC/objc-literal-tests.m Thu Nov 12 09:36:04 2015
@@ -94,4 +94,4 @@ void baz(void) {
   bar(^(void) { return YES; });
 }
 
-// CHECK: attributes [[NUW]] = { nounwind{{.*}} }
+// CHECK: attributes [[NUW]] = { {{(norecurse )?}}nounwind{{.*}} }




More information about the cfe-commits mailing list