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
This commit is contained in:
Nick Hildenbrandt 2002-10-23 18:59:40 +00:00
parent a0877726df
commit 3cecdc5b9a
2 changed files with 26 additions and 6 deletions

View File

@ -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::string;
using std::map;
using std::ostream;
namespace {
class CWriter : public Pass, public InstVisitor<CWriter> {
ostream &Out;
@ -35,6 +35,7 @@ namespace {
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 @@ void CWriter::printModule(Module *M) {
// 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 @@ void CWriter::printModule(Module *M) {
// 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::printContainedStructs(const Type *Ty,
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...

View File

@ -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::string;
using std::map;
using std::ostream;
namespace {
class CWriter : public Pass, public InstVisitor<CWriter> {
ostream &Out;
@ -35,6 +35,7 @@ namespace {
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 @@ void CWriter::printModule(Module *M) {
// 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 @@ void CWriter::printModule(Module *M) {
// 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::printContainedStructs(const Type *Ty,
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...