| 1 |
#include "CxxPreProcessor.h" |
| 2 |
|
| 3 |
#include "file_logger.h" |
| 4 |
|
| 5 |
#include <wx/regex.h> |
| 6 |
|
| 7 |
CxxPreProcessor::CxxPreProcessor() |
| 8 |
: m_options(0) |
| 9 |
, m_maxDepth(-1) |
| 10 |
, m_currentDepth(0) |
| 11 |
{ |
| 12 |
} |
| 13 |
|
| 14 |
CxxPreProcessor::~CxxPreProcessor() {} |
| 15 |
|
| 16 |
void CxxPreProcessor::Parse(const wxFileName& filename, size_t options) |
| 17 |
{ |
| 18 |
try { |
| 19 |
m_options = options; |
| 20 |
std::unordered_set<wxString> V; |
| 21 |
CxxPreProcessorScanner scanner(filename, m_options, V); |
| 22 |
|
| 23 |
// Remove the option so recursive scanner won't get it |
| 24 |
m_options &= ~kLexerOpt_DontCollectMacrosDefinedInThisFile; |
| 25 |
if(!scanner.IsNull()) { |
| 26 |
scanner.Parse(this); |
| 27 |
} |
| 28 |
} catch(CxxLexerException& e) { |
| 29 |
wxUnusedVar(e); |
| 30 |
} |
| 31 |
|
| 32 |
// Delete all the 'deleteOnExit' tokens |
| 33 |
CxxPreProcessorToken::Map_t filteredMap; |
| 34 |
filteredMap.reserve(m_tokens.size()); |
| 35 |
|
| 36 |
for(const auto& p : m_tokens) { |
| 37 |
if(!p.second.deleteOnExit) { |
| 38 |
filteredMap.insert(std::make_pair(p.first, p.second)); |
| 39 |
} |
| 40 |
} |
| 41 |
m_tokens.swap(filteredMap); |
| 42 |
} |
| 43 |
|
| 44 |
bool CxxPreProcessor::ExpandInclude(const wxFileName& currentFile, const wxString& includeStatement, |
| 45 |
wxFileName& outFile) |
| 46 |
{ |
| 47 |
// skip STL debug folders |
| 48 |
if(includeStatement.StartsWith("<debug/")) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
wxString includeName = includeStatement; |
| 53 |
includeName.Replace("\"", ""); |
| 54 |
includeName.Replace("<", ""); |
| 55 |
includeName.Replace(">", ""); |
| 56 |
|
| 57 |
// Try the current file's directory first |
| 58 |
wxArrayString paths = m_includePaths; |
| 59 |
paths.Insert(currentFile.GetPath(), 0); |
| 60 |
|
| 61 |
if(m_noSuchFiles.count(includeStatement)) { |
| 62 |
// wxPrintf("No such file hit\n"); |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
auto iter = m_fileMapping.find(includeStatement); |
| 67 |
if(iter != m_fileMapping.end()) { |
| 68 |
// if this file has a mapped file, it means that we either |
| 69 |
// already scanned it or could not find a match for it |
| 70 |
// wxPrintf("File already been scanned\n"); |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
for(size_t i = 0; i < paths.GetCount(); ++i) { |
| 75 |
wxString tmpfile; |
| 76 |
tmpfile << paths.Item(i) << "/" << includeName; |
| 77 |
wxFileName fn(tmpfile); |
| 78 |
if(fn.FileExists()) { |
| 79 |
fn.MakeAbsolute(); |
| 80 |
m_fileMapping.insert({ includeStatement, fn.GetFullPath() }); |
| 81 |
outFile = fn; |
| 82 |
return true; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// remember that we could not locate this include statement |
| 87 |
m_noSuchFiles.insert(includeStatement); |
| 88 |
m_fileMapping.insert(std::make_pair(includeStatement, wxString())); |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
void CxxPreProcessor::AddIncludePath(const wxString& path) { m_includePaths.Add(path); } |
| 93 |
|
| 94 |
void CxxPreProcessor::AddDefinition(const wxString& def) |
| 95 |
{ |
| 96 |
wxString macroName = def.BeforeFirst('='); |
| 97 |
wxString macroValue = def.AfterFirst('='); |
| 98 |
|
| 99 |
CxxPreProcessorToken token; |
| 100 |
token.name = macroName; |
| 101 |
token.value = macroValue; |
| 102 |
m_tokens.insert(std::make_pair(macroName, token)); |
| 103 |
} |
| 104 |
|
| 105 |
wxArrayString CxxPreProcessor::GetDefinitions() const |
| 106 |
{ |
| 107 |
wxArrayString defs; |
| 108 |
CxxPreProcessorToken::Map_t::const_iterator iter = m_tokens.begin(); |
| 109 |
for(; iter != m_tokens.end(); ++iter) { |
| 110 |
wxString macro; |
| 111 |
macro = iter->second.name; |
| 112 |
if(!iter->second.value.IsEmpty()) { |
| 113 |
macro << "=" << iter->second.value; |
| 114 |
} |
| 115 |
defs.Add(macro); |
| 116 |
} |
| 117 |
return defs; |
| 118 |
} |
| 119 |
|
| 120 |
wxString CxxPreProcessor::GetGxxCommand(const wxString& gxx, const wxString& filename) const |
| 121 |
{ |
| 122 |
wxString command; |
| 123 |
command << gxx << " -dM -E -D__WXMSW__ -D__cplusplus -fsyntax-only "; |
| 124 |
for(size_t i = 0; i < m_includePaths.GetCount(); ++i) { |
| 125 |
command << "-I" << m_includePaths.Item(i) << " "; |
| 126 |
} |
| 127 |
command << " - < " << filename; |
| 128 |
return command; |
| 129 |
} |
| 130 |
|
| 131 |
void CxxPreProcessor::SetIncludePaths(const wxArrayString& includePaths) |
| 132 |
{ |
| 133 |
m_includePaths.Clear(); |
| 134 |
m_includePaths.reserve(includePaths.size()); |
| 135 |
|
| 136 |
std::unordered_set<wxString> S; |
| 137 |
S.reserve(includePaths.size()); |
| 138 |
|
| 139 |
for(size_t i = 0; i < includePaths.GetCount(); ++i) { |
| 140 |
wxString path = includePaths.Item(i); |
| 141 |
path.Trim().Trim(false); |
| 142 |
if(path.IsEmpty()) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
if(S.count(path) == 0) { |
| 147 |
m_includePaths.Add(path); |
| 148 |
S.insert(path); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
bool CxxPreProcessor::CanGoDeeper() const |
| 154 |
{ |
| 155 |
if(m_maxDepth == -1) |
| 156 |
return true; |
| 157 |
return m_currentDepth < m_maxDepth; |
| 158 |
} |
| 159 |
|
| 160 |
void CxxPreProcessor::DecDepth() |
| 161 |
{ |
| 162 |
m_currentDepth--; |
| 163 |
if(m_currentDepth < 0) { |
| 164 |
m_currentDepth = 0; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void CxxPreProcessor::IncDepth() { m_currentDepth++; } |