Add a method "getMainFunction()" that efficiently locates 'main' in a module
authorChris Lattner <sabre@nondot.org>
Fri, 8 Nov 2002 20:34:02 +0000 (20:34 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 8 Nov 2002 20:34:02 +0000 (20:34 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4629 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Module.h
lib/VMCore/Module.cpp

index 2b8c0ab5d0eb1c80cd7d06b3eea923a056a4d47d..6f7a14f410be25ba40cb7f12c7776fc8f198e3ea 100644 (file)
@@ -80,6 +80,12 @@ public:
   ///
   Function *getFunction(const std::string &Name, const FunctionType *Ty);
 
+  /// 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 *getMainFunction();
+
   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
   /// there is already an entry for this name, true is returned and the symbol
   /// table is not modified.
index 397b5e28341d5cae70373ff37d2c2297978392d9..fec6fec77d376428d6c23d10b85675bb8b3d2af9 100644 (file)
@@ -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.
 //