Malloc prototyping now works even if the original file had its own prototype for malloc
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4271 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp
index b7207d5..4227114 100644
--- a/lib/Target/CBackend/CBackend.cpp
+++ b/lib/Target/CBackend/CBackend.cpp
@@ -3,7 +3,6 @@
// This library converts LLVM code to C code, compilable by GCC.
//
//===-----------------------------------------------------------------------==//
-
#include "llvm/Assembly/CWriter.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
@@ -28,6 +27,7 @@
using std::map;
using std::ostream;
+
namespace {
class CWriter : public Pass, public InstVisitor<CWriter> {
ostream &Out;
@@ -35,6 +35,7 @@
const Module *TheModule;
map<const Type *, string> TypeNames;
std::set<const Value*> MangledGlobals;
+ bool needsMalloc;
map<const ConstantFP *, unsigned> FPConstantMap;
public:
@@ -549,8 +550,6 @@
// Global variable declarations...
if (!M->gempty()) {
Out << "\n/* External Global Variable Declarations */\n";
- // Needed for malloc to work on sun.
- Out << "extern void * malloc(size_t);\n";
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
if (I->hasExternalLinkage()) {
Out << "extern ";
@@ -563,12 +562,19 @@
// Function declarations
if (!M->empty()) {
Out << "\n/* Function Declarations */\n";
+ needsMalloc = true;
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
printFunctionSignature(I, true);
Out << ";\n";
}
}
+ // Print Malloc prototype if needed
+ if (needsMalloc){
+ Out << "\n/* Malloc to make sun happy */\n";
+ Out << "extern void * malloc(size_t);\n\n";
+ }
+
// Output the global variable definitions and contents...
if (!M->gempty()) {
Out << "\n\n/* Global Variable Definitions and Initialization */\n";
@@ -673,6 +679,10 @@
void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
+ // If the program provides it's own malloc prototype we don't need
+ // to include the general one.
+ if (getValueName(F) == "malloc")
+ needsMalloc = false;
if (F->hasInternalLinkage()) Out << "static ";
// Loop over the arguments, printing them...