Move code for parsing a cpp-file from the big loop to separate function

Alex
This commit is contained in:
Alex Neundorf 2011-08-10 21:00:53 +02:00
parent 735a5bb321
commit ace121534d
2 changed files with 186 additions and 183 deletions

View File

@ -375,33 +375,6 @@ bool cmQtAutomoc::RunAutomocQt4()
// key = moc source filepath, value = moc output filename // key = moc source filepath, value = moc output filename
std::map<std::string, std::string> notIncludedMocs; std::map<std::string, std::string> notIncludedMocs;
cmsys::RegularExpression mocIncludeRegExp(
"[\n][ \t]*#[ \t]*include[ \t]+"
"[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
cmsys::RegularExpression qObjectRegExp("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
std::list<std::string> headerExtensions;
#if defined(_WIN32)
// not case sensitive
headerExtensions.push_back(".h");
headerExtensions.push_back(".hpp");
headerExtensions.push_back(".hxx");
#elif defined(__APPLE__)
headerExtensions.push_back(".h");
headerExtensions.push_back(".hpp");
headerExtensions.push_back(".hxx");
// detect case-sensitive filesystem
long caseSensitive = pathconf(this->Srcdir.c_str(), _PC_CASE_SENSITIVE);
if (caseSensitive == 1)
{
headerExtensions.push_back(".H");
}
#else
headerExtensions.push_back(".h");
headerExtensions.push_back(".hpp");
headerExtensions.push_back(".hxx");
headerExtensions.push_back(".H");
#endif
std::vector<std::string> sourceFiles; std::vector<std::string> sourceFiles;
cmSystemTools::ExpandListArgument(this->Sources, sourceFiles); cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
@ -415,12 +388,128 @@ bool cmQtAutomoc::RunAutomocQt4()
{ {
printf("Checking -%s-\n", absFilename.c_str()); printf("Checking -%s-\n", absFilename.c_str());
} }
this->ParseCppFile(absFilename, includedMocs, notIncludedMocs);
}
std::vector<std::string> headerFiles;
cmSystemTools::ExpandListArgument(this->Headers, headerFiles);
for (std::vector<std::string>::const_iterator it = headerFiles.begin();
it != headerFiles.end();
++it)
{
const std::string &absFilename = *it;
if (this->Verbose)
{
printf("Checking -%s-\n", absFilename.c_str());
}
if (includedMocs.find(absFilename) == includedMocs.end()
&& notIncludedMocs.find(absFilename) == notIncludedMocs.end())
{
// if this header is not getting processed yet and is explicitly
// mentioned for the automoc the moc is run unconditionally on the
// header and the resulting file is included in the _automoc.cpp file
// (unless there's a .cpp file later on that includes the moc from
// this header)
const std::string currentMoc = "moc_" + cmsys::SystemTools::
GetFilenameWithoutLastExtension(absFilename) + ".cpp";
notIncludedMocs[absFilename] = currentMoc;
}
}
// run moc on all the moc's that are #included in source files
for(std::map<std::string, std::string>::const_iterator
it = includedMocs.begin();
it != includedMocs.end();
++it)
{
this->GenerateMoc(it->first, it->second);
}
std::stringstream outStream(std::stringstream::out);
outStream << "/* This file is autogenerated, do not edit*/\n";
bool automocCppChanged = false;
if (notIncludedMocs.empty())
{
outStream << "enum some_compilers { need_more_than_nothing };\n";
}
else
{
// run moc on the remaining headers and include them in
// the _automoc.cpp file
for(std::map<std::string, std::string>::const_iterator
it = notIncludedMocs.begin();
it != notIncludedMocs.end();
++it)
{
bool mocSuccess = this->GenerateMoc(it->first, it->second);
if (mocSuccess)
{
automocCppChanged = true;
}
outStream << "#include \"" << it->second << "\"\n";
}
}
if (this->RunMocFailed)
{
std::cerr << "returning failed.."<< std::endl;
return false;
}
outStream.flush();
std::string automocSource = outStream.str();
if (!automocCppChanged)
{
// compare contents of the _automoc.cpp file
const std::string oldContents = this->ReadAll(this->OutMocCppFilename);
if (oldContents == automocSource)
{
// nothing changed: don't touch the _automoc.cpp file
return true;
}
}
// source file that includes all remaining moc files (_automoc.cpp file)
std::fstream outfile;
outfile.open(this->OutMocCppFilename.c_str(),
std::ios_base::out | std::ios_base::trunc);
outfile << automocSource;
outfile.close();
return true;
}
void cmQtAutomoc::ParseCppFile(const std::string& absFilename,
std::map<std::string, std::string>& includedMocs,
std::map<std::string, std::string>& notIncludedMocs)
{
cmsys::RegularExpression mocIncludeRegExp(
"[\n][ \t]*#[ \t]*include[ \t]+"
"[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
cmsys::RegularExpression qObjectRegExp("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
std::list<std::string> headerExtensions;
headerExtensions.push_back(".h");
headerExtensions.push_back(".hpp");
headerExtensions.push_back(".hxx");
#if defined(_WIN32)
// not case sensitive, don't add ".H"
#elif defined(__APPLE__)
// detect case-sensitive filesystem
long caseSensitive = pathconf(this->Srcdir.c_str(), _PC_CASE_SENSITIVE);
if (caseSensitive == 1)
{
headerExtensions.push_back(".H");
}
#else
headerExtensions.push_back(".H");
#endif
const std::string contentsString = this->ReadAll(absFilename); const std::string contentsString = this->ReadAll(absFilename);
if (contentsString.empty()) if (contentsString.empty())
{ {
std::cerr << "automoc4: empty source file: " << absFilename << std::endl; std::cerr << "automoc4: empty source file: " << absFilename << std::endl;
continue; return;
} }
const std::string absPath = cmsys::SystemTools::GetFilenamePath( const std::string absPath = cmsys::SystemTools::GetFilenamePath(
cmsys::SystemTools::GetRealPath(absFilename.c_str())) + '/'; cmsys::SystemTools::GetRealPath(absFilename.c_str())) + '/';
@ -432,8 +521,7 @@ bool cmQtAutomoc::RunAutomocQt4()
// the .h nevertheless // the .h nevertheless
const std::string basename = const std::string basename =
cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename); cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
for(std::list<std::string>::const_iterator ext = for(std::list<std::string>::const_iterator ext = headerExtensions.begin();
headerExtensions.begin();
ext != headerExtensions.end(); ext != headerExtensions.end();
++ext) ++ext)
{ {
@ -452,8 +540,7 @@ bool cmQtAutomoc::RunAutomocQt4()
break; break;
} }
} }
for(std::list<std::string>::const_iterator ext = for(std::list<std::string>::const_iterator ext = headerExtensions.begin();
headerExtensions.begin();
ext != headerExtensions.end(); ext != headerExtensions.end();
++ext) ++ext)
{ {
@ -571,94 +658,6 @@ bool cmQtAutomoc::RunAutomocQt4()
matchOffset += mocIncludeRegExp.end(); matchOffset += mocIncludeRegExp.end();
} while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset)); } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
} }
}
std::vector<std::string> headerFiles;
cmSystemTools::ExpandListArgument(this->Headers, headerFiles);
for (std::vector<std::string>::const_iterator it = headerFiles.begin();
it != headerFiles.end();
++it)
{
const std::string &absFilename = *it;
if (this->Verbose)
{
printf("Checking -%s-\n", absFilename.c_str());
}
if (includedMocs.find(absFilename) == includedMocs.end()
&& notIncludedMocs.find(absFilename) == notIncludedMocs.end())
{
// if this header is not getting processed yet and is explicitly
// mentioned for the automoc the moc is run unconditionally on the
// header and the resulting file is included in the _automoc.cpp file
// (unless there's a .cpp file later on that includes the moc from
// this header)
const std::string currentMoc = "moc_" + cmsys::SystemTools::
GetFilenameWithoutLastExtension(absFilename) + ".cpp";
notIncludedMocs[absFilename] = currentMoc;
}
}
// run moc on all the moc's that are #included in source files
for(std::map<std::string, std::string>::const_iterator
it = includedMocs.begin();
it != includedMocs.end();
++it)
{
this->GenerateMoc(it->first, it->second);
}
std::stringstream outStream(std::stringstream::out);
outStream << "/* This file is autogenerated, do not edit*/\n";
bool automocCppChanged = false;
if (notIncludedMocs.empty())
{
outStream << "enum some_compilers { need_more_than_nothing };\n";
}
else
{
// run moc on the remaining headers and include them in
// the _automoc.cpp file
for(std::map<std::string, std::string>::const_iterator
it = notIncludedMocs.begin();
it != notIncludedMocs.end();
++it)
{
bool mocSuccess = this->GenerateMoc(it->first, it->second);
if (mocSuccess)
{
automocCppChanged = true;
}
outStream << "#include \"" << it->second << "\"\n";
}
}
if (this->RunMocFailed)
{
std::cerr << "returning failed.."<< std::endl;
return false;
}
outStream.flush();
std::string automocSource = outStream.str();
if (!automocCppChanged)
{
// compare contents of the _automoc.cpp file
const std::string oldContents = this->ReadAll(this->OutMocCppFilename);
if (oldContents == automocSource)
{
// nothing changed: don't touch the _automoc.cpp file
return true;
}
}
// source file that includes all remaining moc files (_automoc.cpp file)
std::fstream outfile;
outfile.open(this->OutMocCppFilename.c_str(),
std::ios_base::out | std::ios_base::trunc);
outfile << automocSource;
outfile.close();
return true;
} }

View File

@ -29,6 +29,10 @@ private:
bool RunAutomocQt4(); bool RunAutomocQt4();
bool GenerateMoc(const std::string& sourceFile, bool GenerateMoc(const std::string& sourceFile,
const std::string& mocFileName); const std::string& mocFileName);
void ParseCppFile(const std::string& absFilename,
std::map<std::string, std::string>& includedMocs,
std::map<std::string, std::string>& notIncludedMocs);
void Init(); void Init();
std::string Join(const std::list<std::string>& lst, char separator); std::string Join(const std::list<std::string>& lst, char separator);