| 1 |
#include "CxxUsingNamespaceCollector.h" |
| 2 |
#include "CxxPreProcessor.h" |
| 3 |
#include "CxxScannerTokens.h" |
| 4 |
|
| 5 |
CxxUsingNamespaceCollector::CxxUsingNamespaceCollector(CxxPreProcessor* preProcessor, const wxFileName& filename, |
| 6 |
std::unordered_set<wxString>& visitedFiles) |
| 7 |
: CxxScannerBase(preProcessor, filename) |
| 8 |
, m_visitedFiles(visitedFiles) |
| 9 |
{ |
| 10 |
} |
| 11 |
|
| 12 |
CxxUsingNamespaceCollector::~CxxUsingNamespaceCollector() {} |
| 13 |
|
| 14 |
void CxxUsingNamespaceCollector::OnToken(CxxLexerToken& token) |
| 15 |
{ |
| 16 |
switch(token.GetType()) { |
| 17 |
case T_PP_INCLUDE_FILENAME: { |
| 18 |
// we found an include statement, recurse into it |
| 19 |
wxFileName include; |
| 20 |
if(m_preProcessor->CanGoDeeper() && m_preProcessor->ExpandInclude(m_filename, token.GetText(), include) && |
| 21 |
m_visitedFiles.count(include.GetFullPath()) == 0) { |
| 22 |
m_visitedFiles.insert(include.GetFullPath()); |
| 23 |
CxxUsingNamespaceCollector scanner{ m_preProcessor, include, m_visitedFiles }; |
| 24 |
m_preProcessor->IncDepth(); |
| 25 |
scanner.Parse(); |
| 26 |
m_preProcessor->DecDepth(); |
| 27 |
|
| 28 |
// Append the matched results to the current parser results |
| 29 |
for(const auto& ns : scanner.GetUsingNamespaces()) { |
| 30 |
if(!ns.empty() && m_usingNamespaces.Index(ns) == wxNOT_FOUND) { |
| 31 |
m_usingNamespaces.Add(ns); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
break; |
| 36 |
} |
| 37 |
case T_USING: |
| 38 |
ParseUsingNamespace(); |
| 39 |
break; |
| 40 |
default: |
| 41 |
break; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
void CxxUsingNamespaceCollector::ParseUsingNamespace() |
| 46 |
{ |
| 47 |
// Get the next token |
| 48 |
CxxLexerToken token; |
| 49 |
if(!::LexerNext(m_scanner, token)) |
| 50 |
return; |
| 51 |
if(token.GetType() != T_NAMESPACE) |
| 52 |
return; |
| 53 |
|
| 54 |
// Read everything until we find the ';' |
| 55 |
wxString usingNamespace; |
| 56 |
while(::LexerNext(m_scanner, token)) { |
| 57 |
if(token.GetType() == ';') { |
| 58 |
break; |
| 59 |
} |
| 60 |
usingNamespace << token.GetText(); |
| 61 |
} |
| 62 |
|
| 63 |
if(!usingNamespace.IsEmpty() && m_usingNamespaces.Index(usingNamespace) == wxNOT_FOUND) { |
| 64 |
m_usingNamespaces.Add(usingNamespace); |
| 65 |
} |
| 66 |
} |