[llvm-commits] CVS: llvm/lib/VMCore/Module.cpp

Chris Lattner lattner at cs.uiuc.edu
Fri Nov 8 14:35:06 PST 2002


Changes in directory llvm/lib/VMCore:

Module.cpp updated: 1.30 -> 1.31

---
Log message:

Add a method "getMainFunction()" that efficiently locates 'main' in a module



---
Diffs of the changes:

Index: llvm/lib/VMCore/Module.cpp
diff -u llvm/lib/VMCore/Module.cpp:1.30 llvm/lib/VMCore/Module.cpp:1.31
--- llvm/lib/VMCore/Module.cpp:1.30	Tue Oct 15 16:26:29 2002
+++ llvm/lib/VMCore/Module.cpp	Fri Nov  8 14:34:02 2002
@@ -139,6 +139,58 @@
   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.
 //





More information about the llvm-commits mailing list