diff --git a/driver/cl_options.cpp b/driver/cl_options.cpp index c88a2633..61c389bd 100644 --- a/driver/cl_options.cpp +++ b/driver/cl_options.cpp @@ -204,6 +204,10 @@ static cl::list stringImportPaths("J", cl::location(strImpPathStore), cl::Prefix); +static cl::opt addMain("main", + cl::desc("add empty main() (e.g. for unittesting)"), + cl::ZeroOrMore, + cl::location(global.params.addMain)); // -d-debug is a bit messy, it has 3 modes: diff --git a/driver/main.cpp b/driver/main.cpp index ae930a55..b52c1f3d 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -693,6 +693,12 @@ int main(int argc, char** argv) } } + if (global.params.addMain) + { + // a dummy name, we never actually look up this file + files.push(const_cast(global.main_d)); + } + // Create Modules Modules modules; modules.reserve(files.dim); @@ -813,7 +819,18 @@ int main(int argc, char** argv) if (!Module::rootModule) Module::rootModule = m; m->importedFrom = m; - m->read(Loc()); + + if (strcmp(m->srcfile->name->str, global.main_d) == 0) + { + static const char buf[] = "void main(){}"; + m->srcfile->setbuffer((void *)buf, sizeof(buf)); + m->srcfile->ref = 1; + } + else + { + m->read(Loc()); + } + m->parse(global.params.doDocComments); m->buildTargetFiles(singleObj); m->deleteObjFile();