[llvm-commits] [llvm-gcc-4.2] r84829 - /llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp

Chris Lattner sabre at nondot.org
Wed Oct 21 21:47:07 PDT 2009


Author: lattner
Date: Wed Oct 21 23:47:07 2009
New Revision: 84829

URL: http://llvm.org/viewvc/llvm-project?rev=84829&view=rev
Log:
add a somewhat despicable (but precendented) hack to fix PR4678 & rdar://7309675.

Basically, we want to compile C/ObjC functions like:

void foo() {

into nullary LLVM IR functions.  This is important so we don't get varargs ABI
stuff, and also important because we want to get "arguments are dropped" warnings
from instcombine if you have calls like "foo(42)".

OTOH, we don't want to do this for C++ functions like:

void foo(...) {

While this foo can't do a va_begin, we do want to compile it to a varargs LLVM IR
function.  Not doing so will cause 'arguments are dropped' warning for "foo(42)",
which (unlike the C case) makes perfect sense.

This is a hack because it is predicated on the language kind.  This is because GCC's
tree structure apparently represents these things exactly the same way, so there is
no way to distinguish them.  However, this is a *precedented* hack, because GCC 
already does this in mudflap, fold-const.c etc.

If someone has a better way to get this information, I'm all ears.  Until then, I'm 
just happy to have this old issue fixed. Testcase here: test/FrontendC++/varargs.cpp.



Modified:
    llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp

Modified: llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp?rev=84829&r1=84828&r2=84829&view=diff

==============================================================================
--- llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp (original)
+++ llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp Wed Oct 21 23:47:07 2009
@@ -404,6 +404,29 @@
   return false;
 }
 
+
+/// isCompilingCCode - Return true if we are compiling C or Objective-C code.
+static bool isCompilingCCode() {
+  // If we've already determined this, return it.
+  static unsigned Val = 2;
+  if (Val != 2) return (bool)Val;
+  
+  StringRef LanguageName = lang_hooks.name;
+  
+  if (LanguageName == "GNU C" || LanguageName == "GNU Objective-C")
+    return (Val = true);
+  
+  if (LanguageName == "GNU C++" ||
+      LanguageName == "GNU Ada" ||
+      LanguageName == "GNU F77" ||
+      LanguageName == "GNU Pascal" ||
+      LanguageName == "GNU Java" ||
+      LanguageName == "GNU Objective-C++")
+    return (Val = false);
+
+  return (Val = true);
+}
+
 void TreeToLLVM::StartFunctionBody() {
   const char *Name = "";
   // Get the name of the function.
@@ -421,7 +444,10 @@
   // allows C functions declared as "T foo() {}" to be treated like
   // "T foo(void) {}" and allows us to handle functions with K&R-style
   // definitions correctly.
-  if (TYPE_ARG_TYPES(TREE_TYPE(FnDecl)) == 0) {
+  //
+  // Note that we only do this in C/Objective-C.  Doing this in C++ for
+  // functions explicitly declared as taking (...) is bad.
+  if (TYPE_ARG_TYPES(TREE_TYPE(FnDecl)) == 0 && isCompilingCCode()) {
     FTy = TheTypeConverter->ConvertArgListToFnType(TREE_TYPE(FnDecl),
                                                    DECL_ARGUMENTS(FnDecl),
                                                    static_chain,





More information about the llvm-commits mailing list