Skip to content

Commit 894b572

Browse files
committed
Improve finding DCEs for nested include files.
1 parent c76ba26 commit 894b572

File tree

3 files changed

+82
-48
lines changed

3 files changed

+82
-48
lines changed

include/projectGenerator.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,17 @@ class ProjectGenerator
349349
bool bit64Only = false) const;
350350

351351
void outputSourceFiles(string& projectTemplate, string& filterTemplate);
352+
353+
/**
354+
* Find and load the list of function exports prefixes.
355+
* @param [out] exportPrefixes The list of loaded export prefixes.
356+
*/
357+
bool findExportsList(StaticList& exportPrefixes) const;
352358

359+
/**
360+
* Generate a module definition file.
361+
* @param includeDirs The list of current directories to look for included files.
362+
*/
353363
bool outputProjectExports(const StaticList& includeDirs) const;
354364

355365
/**

source/projectGenerator.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,9 +1089,8 @@ void ProjectGenerator::outputSourceFiles(string& projectTemplate, string& filter
10891089
filterTemplate.insert(findPosFilt, addFilters);
10901090
}
10911091

1092-
bool ProjectGenerator::outputProjectExports(const StaticList& includeDirs) const
1092+
bool ProjectGenerator::findExportsList(StaticList& exportPrefixes) const
10931093
{
1094-
outputLine(" Generating project exports file (" + m_projectName + ")...");
10951094
string exportList;
10961095
if (!findFile(this->m_projectDir + "/*.v", exportList)) {
10971096
outputError("Failed finding project exports (" + m_projectName + ")");
@@ -1103,8 +1102,7 @@ bool ProjectGenerator::outputProjectExports(const StaticList& includeDirs) const
11031102
loadFromFile(this->m_projectDir + exportList, exportsFile);
11041103

11051104
// Search for start of global tag
1106-
string global = "global:";
1107-
StaticList exportStrings;
1105+
const string global = "global:";
11081106
uint findPos = exportsFile.find(global);
11091107
if (findPos != string::npos) {
11101108
// Remove everything outside the global section
@@ -1128,14 +1126,24 @@ bool ProjectGenerator::outputProjectExports(const StaticList& includeDirs) const
11281126
findPos = 0;
11291127
findPos2 = exportsFile.find(';');
11301128
while (findPos2 != string::npos) {
1131-
exportStrings.push_back(exportsFile.substr(findPos, findPos2 - findPos));
1129+
exportPrefixes.push_back(exportsFile.substr(findPos, findPos2 - findPos));
11321130
findPos = findPos2 + 1;
11331131
findPos2 = exportsFile.find(';', findPos);
11341132
}
1133+
return true;
11351134
} else {
11361135
outputError("Failed finding global start in project exports (" + exportList + ")");
11371136
return false;
11381137
}
1138+
}
1139+
1140+
bool ProjectGenerator::outputProjectExports(const StaticList& includeDirs) const
1141+
{
1142+
outputLine(" Generating project exports file (" + m_projectName + ")...");
1143+
StaticList exportStrings;
1144+
if (!findExportsList(exportStrings)) {
1145+
return false;
1146+
}
11391147

11401148
// Split each source file into different directories to avoid name clashes
11411149
map<string, StaticList> directoryObjects;
@@ -1195,7 +1203,7 @@ bool ProjectGenerator::outputProjectExports(const StaticList& includeDirs) const
11951203
// ID is a 2 or 3 character sequence used to uniquely identify the object
11961204

11971205
// Check if it is a wild card search
1198-
findPos = j.find('*');
1206+
uint findPos = j.find('*');
11991207
if (findPos != string::npos) {
12001208
// Strip the wild card (Note: assumes wild card is at the end!)
12011209
string search = j.substr(0, findPos);
@@ -1288,7 +1296,7 @@ bool ProjectGenerator::outputProjectExports(const StaticList& includeDirs) const
12881296
// Search through file for module exports
12891297
for (const auto& j : exportStrings) {
12901298
// Check if it is a wild card search
1291-
findPos = j.find('*');
1299+
uint findPos = j.find('*');
12921300
const string invalidChars = ",.(){}[]`'\"+-*/!@#$%^&*<>|;\\= \r\n\t";
12931301
if (findPos != string::npos) {
12941302
// Strip the wild card (Note: assumes wild card is at the end!)

source/projectGenerator_dce.cpp

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,63 +64,73 @@ bool ProjectGenerator::outputProjectDCE(const StaticList& includeDirs)
6464
if (!loadFromFile(*itFile, file)) {
6565
return false;
6666
}
67-
bool requiresPreProcess = false;
68-
outputProjectDCEFindFunctions(file, *itFile, foundDCEUsage, requiresPreProcess, nonDCEUsage);
69-
if (requiresPreProcess) {
70-
preProcFiles.push_back(*itFile);
71-
}
7267

7368
// Check if this file includes additional source files
7469
vector<string> extensions = {".c\"", ".h\""};
7570
for (auto& ext : extensions) {
7671
uint findPos = file.find(ext);
7772
while (findPos != string::npos) {
7873
// Check if this is an include
79-
uint findPos2 = file.rfind("#include \"", findPos);
80-
if ((findPos2 != string::npos) && (findPos - findPos2 < 50)) {
81-
// Get the name of the file
82-
findPos2 += 10;
83-
findPos += 2;
84-
string templateFile = file.substr(findPos2, findPos - findPos2);
85-
// Split filename from any directory structures
86-
replace(templateFile.begin(), templateFile.end(), '\\', '/');
87-
uint projName = templateFile.rfind(m_projectName + '/');
88-
if (projName != string::npos) {
89-
templateFile = templateFile.substr(projName + 1 + m_projectName.length());
90-
}
91-
if (templateFile.length() >= 3) {
92-
string found;
93-
string back = templateFile;
94-
templateFile = m_projectDir + back;
95-
if (!findFile(templateFile, found)) {
96-
templateFile = (m_configHelper.m_rootDirectory.length() > 0) ?
97-
m_configHelper.m_rootDirectory + '/' + back :
98-
back;
74+
uint findPos3 = file.rfind("#include", findPos);
75+
if (findPos3 != string::npos) {
76+
uint findPos2 = file.find_first_not_of(g_whiteSpace, findPos3 + 8);
77+
if (findPos2 != string::npos && file[findPos2] == '\"' &&
78+
file.find_first_of("\".", findPos2 + 2) == findPos) {
79+
// Get the name of the file
80+
++findPos2;
81+
findPos += 2;
82+
string templateFile = file.substr(findPos2, findPos - findPos2);
83+
// Split filename from any directory structures
84+
replace(templateFile.begin(), templateFile.end(), '\\', '/');
85+
uint projName = templateFile.rfind(m_projectName + '/');
86+
if (projName != string::npos) {
87+
templateFile = templateFile.substr(projName + 1 + m_projectName.length());
88+
}
89+
if (templateFile.length() >= 3) {
90+
string found;
91+
string back = templateFile;
92+
templateFile = m_projectDir + back;
9993
if (!findFile(templateFile, found)) {
100-
templateFile = m_configHelper.m_solutionDirectory + m_projectName + '/' + back;
94+
templateFile = (m_configHelper.m_rootDirectory.length() > 0) ?
95+
m_configHelper.m_rootDirectory + '/' + back :
96+
back;
10197
if (!findFile(templateFile, found)) {
102-
templateFile = itFile->substr(0, itFile->rfind('/') + 1) + back;
98+
templateFile = m_configHelper.m_solutionDirectory + m_projectName + '/' + back;
10399
if (!findFile(templateFile, found)) {
104-
templateFile = m_configHelper.m_solutionDirectory + back;
100+
templateFile = itFile->substr(0, itFile->rfind('/') + 1) + back;
105101
if (!findFile(templateFile, found)) {
106-
// Fail only if this is a c file
107-
if (ext == extensions[0]) {
108-
outputError("Failed to find included file " + back);
109-
return false;
102+
templateFile = m_configHelper.m_solutionDirectory + back;
103+
if (!findFile(templateFile, found)) {
104+
// Fail only if this is a c file
105+
if (ext == extensions[0]) {
106+
outputError("Failed to find included file " + back);
107+
return false;
108+
}
109+
templateFile = "";
110110
}
111-
templateFile = "";
112111
}
113112
}
114113
}
115114
}
116-
}
117-
// Add the file to the list
118-
if (templateFile.length() >= 3) {
119-
if (templateFile.find("./") == 0) {
120-
templateFile = templateFile.substr(2);
121-
}
122-
if (find(searchFiles.begin(), searchFiles.end(), templateFile) == searchFiles.end()) {
123-
searchFiles.push_back(templateFile);
115+
// Add the file to the list
116+
if (templateFile.length() >= 3) {
117+
if (templateFile.find("./") == 0) {
118+
templateFile = templateFile.substr(2);
119+
}
120+
if (ext == extensions[0]) {
121+
// If it's a source file then insert directly into existing file so we can find any
122+
// nested DCE between files
123+
string file2;
124+
if (!loadFromFile(templateFile, file2, false, false)) {
125+
outputError("Failed to open included file " + templateFile);
126+
return false;
127+
}
128+
file.replace(findPos3, findPos + 1, file2);
129+
findPos += file2.length();
130+
} else if (find(searchFiles.begin(), searchFiles.end(), templateFile) ==
131+
searchFiles.end()) {
132+
searchFiles.push_back(templateFile);
133+
}
124134
}
125135
}
126136
}
@@ -129,6 +139,12 @@ bool ProjectGenerator::outputProjectDCE(const StaticList& includeDirs)
129139
findPos = file.find(ext, findPos + 1);
130140
}
131141
}
142+
143+
bool requiresPreProcess = false;
144+
outputProjectDCEFindFunctions(file, *itFile, foundDCEUsage, requiresPreProcess, nonDCEUsage);
145+
if (requiresPreProcess) {
146+
preProcFiles.push_back(*itFile);
147+
}
132148
}
133149
#if !FORCEALLDCE
134150
// Get a list of all files in current project directory (including subdirectories)

0 commit comments

Comments
 (0)