summaryrefslogtreecommitdiffstats
path: root/llvm/lib/VMCore/Module.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2002-11-08 20:34:02 +0000
committerChris Lattner <sabre@nondot.org>2002-11-08 20:34:02 +0000
commit1f985e0d3c16f224d84ff8478a3b0c429676be87 (patch)
treed41a306d3e0b31f1693d1d98eb93bbc7502a539e /llvm/lib/VMCore/Module.cpp
parent465668552ab0d120fa71a6d26385947caa92883e (diff)
downloadbcm5719-llvm-1f985e0d3c16f224d84ff8478a3b0c429676be87.tar.gz
bcm5719-llvm-1f985e0d3c16f224d84ff8478a3b0c429676be87.zip
Add a method "getMainFunction()" that efficiently locates 'main' in a module
llvm-svn: 4629
Diffstat (limited to 'llvm/lib/VMCore/Module.cpp')
-rw-r--r--llvm/lib/VMCore/Module.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/llvm/lib/VMCore/Module.cpp b/llvm/lib/VMCore/Module.cpp
index 397b5e28341..fec6fec77d3 100644
--- a/llvm/lib/VMCore/Module.cpp
+++ b/llvm/lib/VMCore/Module.cpp
@@ -139,6 +139,58 @@ bool Module::addTypeName(const std::string &Name, const Type *Ty) {
return false;
}
+/// getMainFunction - This function looks up main efficiently. This is such a
+/// common case, that it is a method in Module. If main cannot be found, a
+/// null pointer is returned.
+///
+Function *Module::getMainFunction() {
+ std::vector<const Type*> Params;
+
+ // int main(void)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
+ Params, false)))
+ return F;
+
+ // void main(void)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
+ Params, false)))
+ return F;
+
+ Params.push_back(Type::IntTy);
+
+ // int main(int argc)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
+ Params, false)))
+ return F;
+
+ // void main(int argc)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
+ Params, false)))
+ return F;
+
+ for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
+ Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
+
+ // int main(int argc, char **argv)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
+ Params, false)))
+ return F;
+
+ // void main(int argc, char **argv)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
+ Params, false)))
+ return F;
+ }
+
+ // Loop over all of the methods, trying to find main the hard way...
+ for (iterator I = begin(), E = end(); I != E; ++I)
+ if (I->getName() == "main")
+ return I;
+ return 0; // Main not found...
+}
+
+
+
// getTypeName - If there is at least one entry in the symbol table for the
// specified type, return it.
//
OpenPOWER on IntegriCloud