| 1 |
#include "CxxPreProcessorScanner.h" |
| 2 |
|
| 3 |
#include "CxxPreProcessor.h" |
| 4 |
#include "CxxPreProcessorExpression.h" |
| 5 |
#include "CxxScannerTokens.h" |
| 6 |
#include "file_logger.h" |
| 7 |
|
| 8 |
#include <wx/sharedptr.h> |
| 9 |
|
| 10 |
CxxPreProcessorScanner::CxxPreProcessorScanner(const wxFileName& filename, size_t options, |
| 11 |
std::unordered_set<wxString>& visitedFiles) |
| 12 |
: m_scanner(NULL) |
| 13 |
, m_filename(filename) |
| 14 |
, m_options(options) |
| 15 |
, m_visitedFiles(visitedFiles) |
| 16 |
{ |
| 17 |
m_scanner = ::LexerNew(m_filename, m_options); |
| 18 |
wxASSERT(m_scanner); |
| 19 |
} |
| 20 |
|
| 21 |
CxxPreProcessorScanner::~CxxPreProcessorScanner() |
| 22 |
{ |
| 23 |
if(m_scanner) { |
| 24 |
::LexerDestroy(&m_scanner); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
void CxxPreProcessorScanner::GetRestOfPPLine(wxString& rest, bool collectNumberOnly) |
| 29 |
{ |
| 30 |
CxxLexerToken token; |
| 31 |
bool numberFound = false; |
| 32 |
while(m_scanner && ::LexerNext(m_scanner, token) && token.GetType() != T_PP_STATE_EXIT) { |
| 33 |
if(!numberFound && collectNumberOnly) { |
| 34 |
if(token.GetType() == T_PP_DEC_NUMBER || token.GetType() == T_PP_OCTAL_NUMBER || |
| 35 |
token.GetType() == T_PP_HEX_NUMBER || token.GetType() == T_PP_FLOAT_NUMBER) { |
| 36 |
rest = token.GetWXString(); |
| 37 |
numberFound = true; |
| 38 |
} |
| 39 |
} else if(!collectNumberOnly) { |
| 40 |
rest << " " << token.GetWXString(); |
| 41 |
} |
| 42 |
} |
| 43 |
rest.Trim(false).Trim(true); |
| 44 |
} |
| 45 |
|
| 46 |
bool CxxPreProcessorScanner::ConsumeBlock() |
| 47 |
{ |
| 48 |
CxxLexerToken token; |
| 49 |
int depth = 1; |
| 50 |
while(m_scanner && ::LexerNext(m_scanner, token)) { |
| 51 |
switch(token.GetType()) { |
| 52 |
case T_PP_ENDIF: |
| 53 |
depth--; |
| 54 |
if(depth == 0) { |
| 55 |
return true; |
| 56 |
} |
| 57 |
break; |
| 58 |
case T_PP_IF: |
| 59 |
case T_PP_IFDEF: |
| 60 |
case T_PP_IFNDEF: |
| 61 |
depth++; |
| 62 |
break; |
| 63 |
default: |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
// throw CxxLexerException("Invalid EOF"); |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
void CxxPreProcessorScanner::Parse(CxxPreProcessor* pp) |
| 72 |
{ |
| 73 |
CxxLexerToken token; |
| 74 |
bool searchingForBranch = false; |
| 75 |
CxxPreProcessorToken::Map_t& ppTable = pp->GetTokens(); |
| 76 |
while(m_scanner && ::LexerNext(m_scanner, token)) { |
| 77 |
// Pre Processor state |
| 78 |
switch(token.GetType()) { |
| 79 |
case T_PP_INCLUDE_FILENAME: { |
| 80 |
// we found an include statement, recurse into it |
| 81 |
wxFileName include; |
| 82 |
if(pp->ExpandInclude(m_filename, token.GetWXString(), include) && |
| 83 |
m_visitedFiles.count(include.GetFullPath()) == 0) { |
| 84 |
m_visitedFiles.insert(include.GetFullPath()); |
| 85 |
CxxPreProcessorScanner scanner(include, pp->GetOptions(), m_visitedFiles); |
| 86 |
try { |
| 87 |
if(!scanner.IsNull()) { |
| 88 |
scanner.Parse(pp); |
| 89 |
} |
| 90 |
} catch(CxxLexerException& e) { |
| 91 |
// catch the exception |
| 92 |
clDEBUG() << "Exception caught:" << e.message << endl; |
| 93 |
} |
| 94 |
// make sure we always delete the scanner |
| 95 |
LOG_IF_TRACE { clDEBUG1() << "<== Resuming parser on file:" << m_filename << clEndl; } |
| 96 |
} |
| 97 |
break; |
| 98 |
} |
| 99 |
case T_PP_IFDEF: { |
| 100 |
searchingForBranch = true; |
| 101 |
// read the identifier |
| 102 |
ReadUntilMatch(T_PP_IDENTIFIER, token); |
| 103 |
if(IsTokenExists(ppTable, token)) { |
| 104 |
searchingForBranch = false; |
| 105 |
// condition is true |
| 106 |
Parse(pp); |
| 107 |
} else { |
| 108 |
// skip until we find the next: |
| 109 |
// else, elif, endif (but do not consume these tokens) |
| 110 |
if(!ConsumeCurrentBranch()) |
| 111 |
return; |
| 112 |
} |
| 113 |
break; |
| 114 |
} |
| 115 |
case T_PP_IFNDEF: { |
| 116 |
searchingForBranch = true; |
| 117 |
// read the identifier |
| 118 |
ReadUntilMatch(T_PP_IDENTIFIER, token); |
| 119 |
if(!IsTokenExists(ppTable, token)) { |
| 120 |
searchingForBranch = false; |
| 121 |
// condition is true |
| 122 |
Parse(pp); |
| 123 |
} else { |
| 124 |
// skip until we find the next: |
| 125 |
// else, elif, endif (but do not consume these tokens) |
| 126 |
if(!ConsumeCurrentBranch()) |
| 127 |
return; |
| 128 |
} |
| 129 |
break; |
| 130 |
} |
| 131 |
case T_PP_IF: |
| 132 |
searchingForBranch = true; |
| 133 |
case T_PP_ELIF: { |
| 134 |
if(searchingForBranch) { |
| 135 |
// We expect a condition |
| 136 |
if(!CheckIf(ppTable)) { |
| 137 |
// skip until we find the next: |
| 138 |
// else, elif, endif (but do not consume these tokens) |
| 139 |
if(!ConsumeCurrentBranch()) |
| 140 |
return; |
| 141 |
|
| 142 |
} else { |
| 143 |
searchingForBranch = false; |
| 144 |
// condition is true |
| 145 |
Parse(pp); |
| 146 |
} |
| 147 |
} else { |
| 148 |
ConsumeBlock(); |
| 149 |
return; |
| 150 |
} |
| 151 |
break; |
| 152 |
} |
| 153 |
case T_PP_ELSE: { |
| 154 |
if(searchingForBranch) { |
| 155 |
// if we reached an else, it means that we could not match |
| 156 |
// a if/elif/ifdef condition until now - enter it |
| 157 |
Parse(pp); |
| 158 |
searchingForBranch = false; |
| 159 |
} else { |
| 160 |
// we already found the branch for the current block |
| 161 |
// this means that the 'else' is a stop sign for us |
| 162 |
ConsumeBlock(); |
| 163 |
return; |
| 164 |
} |
| 165 |
break; |
| 166 |
} |
| 167 |
case T_PP_ENDIF: { |
| 168 |
return; |
| 169 |
} |
| 170 |
case T_PP_DEFINE: { |
| 171 |
if(!::LexerNext(m_scanner, token) || token.GetType() != T_PP_IDENTIFIER) { |
| 172 |
// Recover |
| 173 |
wxString dummy; |
| 174 |
GetRestOfPPLine(dummy); |
| 175 |
break; |
| 176 |
} |
| 177 |
wxString macroName = token.GetWXString(); |
| 178 |
|
| 179 |
wxString macroValue; |
| 180 |
// Optionally get the value |
| 181 |
GetRestOfPPLine(macroValue, m_options & kLexerOpt_CollectMacroValueNumbers); |
| 182 |
|
| 183 |
CxxPreProcessorToken pp; |
| 184 |
pp.name = macroName; |
| 185 |
pp.value = macroValue; |
| 186 |
// mark this token for deletion when the entire TU parsing is done |
| 187 |
pp.deleteOnExit = (m_options & kLexerOpt_DontCollectMacrosDefinedInThisFile); |
| 188 |
ppTable.insert(std::make_pair(pp.name, pp)); |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
bool CxxPreProcessorScanner::CheckIfDefined(const CxxPreProcessorToken::Map_t& table) |
| 196 |
{ |
| 197 |
CxxLexerToken token; |
| 198 |
if(m_scanner && ::LexerNext(m_scanner, token)) { |
| 199 |
if(token.GetType() == T_PP_STATE_EXIT) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
switch(token.GetType()) { |
| 203 |
case T_PP_IDENTIFIER: |
| 204 |
return table.count(token.GetWXString()); |
| 205 |
case '(': |
| 206 |
// ignore |
| 207 |
break; |
| 208 |
default: |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
#define SET_CUR_EXPR_VALUE_RET_FALSE(v) \ |
| 216 |
if(cur->IsValueSet()) \ |
| 217 |
return false; \ |
| 218 |
else \ |
| 219 |
cur->SetValue((double)v); |
| 220 |
|
| 221 |
struct ExpressionLocker { |
| 222 |
CxxPreProcessorExpression* m_expr; |
| 223 |
ExpressionLocker(CxxPreProcessorExpression* expr) |
| 224 |
: m_expr(expr) |
| 225 |
{ |
| 226 |
} |
| 227 |
~ExpressionLocker() { wxDELETE(m_expr); } |
| 228 |
}; |
| 229 |
|
| 230 |
bool CxxPreProcessorScanner::CheckIf(const CxxPreProcessorToken::Map_t& table) |
| 231 |
{ |
| 232 |
// we currently support |
| 233 |
// #if IDENTIFIER |
| 234 |
// #if NUMBER |
| 235 |
// #if (cond) && (cond) || !(cond) |
| 236 |
// anything else, returns false |
| 237 |
CxxLexerToken token; |
| 238 |
CxxPreProcessorExpression* cur = new CxxPreProcessorExpression(false); |
| 239 |
ExpressionLocker locker(cur); |
| 240 |
CxxPreProcessorExpression* head = cur; |
| 241 |
while(m_scanner && ::LexerNext(m_scanner, token)) { |
| 242 |
if(token.GetType() == T_PP_STATE_EXIT) { |
| 243 |
bool res = head->IsTrue(); |
| 244 |
return res; |
| 245 |
} |
| 246 |
switch(token.GetType()) { |
| 247 |
case '(': |
| 248 |
case ')': |
| 249 |
// ignore parenthesis |
| 250 |
break; |
| 251 |
|
| 252 |
case '!': |
| 253 |
cur->SetNot(); |
| 254 |
break; |
| 255 |
|
| 256 |
case T_PP_DEC_NUMBER: { |
| 257 |
SET_CUR_EXPR_VALUE_RET_FALSE(atol(token.GetText())); |
| 258 |
break; |
| 259 |
} |
| 260 |
case T_PP_OCTAL_NUMBER: { |
| 261 |
SET_CUR_EXPR_VALUE_RET_FALSE(atol(token.GetText())); |
| 262 |
break; |
| 263 |
} |
| 264 |
case T_PP_HEX_NUMBER: { |
| 265 |
SET_CUR_EXPR_VALUE_RET_FALSE(atol(token.GetText())); |
| 266 |
break; |
| 267 |
} |
| 268 |
case T_PP_FLOAT_NUMBER: { |
| 269 |
SET_CUR_EXPR_VALUE_RET_FALSE(atof(token.GetText())); |
| 270 |
break; |
| 271 |
} |
| 272 |
case T_PP_IDENTIFIER: { |
| 273 |
wxString identifier = token.GetWXString(); |
| 274 |
CxxPreProcessorToken::Map_t::const_iterator iter = table.find(identifier); |
| 275 |
if(iter == table.end()) { |
| 276 |
SET_CUR_EXPR_VALUE_RET_FALSE(0); |
| 277 |
} else { |
| 278 |
if(cur->IsDefined()) { |
| 279 |
// no need to test further, it exists (the current expression |
| 280 |
// if a 'defined' statement) |
| 281 |
SET_CUR_EXPR_VALUE_RET_FALSE(1); |
| 282 |
} else { |
| 283 |
wxString macroValue = iter->second.value; |
| 284 |
if(macroValue.IsEmpty()) { |
| 285 |
SET_CUR_EXPR_VALUE_RET_FALSE(0); |
| 286 |
} else { |
| 287 |
long v(0); |
| 288 |
bool res = macroValue.ToCLong(&v); |
| 289 |
if(!res) { |
| 290 |
SET_CUR_EXPR_VALUE_RET_FALSE(0); |
| 291 |
|
| 292 |
} else { |
| 293 |
SET_CUR_EXPR_VALUE_RET_FALSE(v); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
break; |
| 299 |
} |
| 300 |
case T_PP_DEFINED: |
| 301 |
cur->SetDefined(true); |
| 302 |
break; |
| 303 |
case T_PP_AND: |
| 304 |
// And operand |
| 305 |
cur = cur->SetNext(CxxPreProcessorExpression::kAND, new CxxPreProcessorExpression(false)); |
| 306 |
break; |
| 307 |
case T_PP_OR: |
| 308 |
// OR operand |
| 309 |
cur = cur->SetNext(CxxPreProcessorExpression::kOR, new CxxPreProcessorExpression(false)); |
| 310 |
break; |
| 311 |
case T_PP_GT: |
| 312 |
cur = cur->SetNext(CxxPreProcessorExpression::kGreaterThan, new CxxPreProcessorExpression(0)); |
| 313 |
break; |
| 314 |
case T_PP_GTEQ: |
| 315 |
cur = cur->SetNext(CxxPreProcessorExpression::kGreaterThanEqual, new CxxPreProcessorExpression(0)); |
| 316 |
break; |
| 317 |
case T_PP_LT: |
| 318 |
cur = cur->SetNext(CxxPreProcessorExpression::kLowerThan, new CxxPreProcessorExpression(0)); |
| 319 |
break; |
| 320 |
case T_PP_LTEQ: |
| 321 |
cur = cur->SetNext(CxxPreProcessorExpression::kLowerThanEqual, new CxxPreProcessorExpression(0)); |
| 322 |
break; |
| 323 |
default: |
| 324 |
break; |
| 325 |
} |
| 326 |
} |
| 327 |
return false; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* @brief fast-forward until we hit one of the tokens in the stopTokens |
| 332 |
* Note that this function does not consume the 'stopToken' |
| 333 |
*/ |
| 334 |
bool CxxPreProcessorScanner::ConsumeCurrentBranch() |
| 335 |
{ |
| 336 |
CxxLexerToken token; |
| 337 |
int depth = 1; |
| 338 |
// T_PP_ELIF |
| 339 |
// T_PP_ELSE |
| 340 |
// T_PP_ENDIF |
| 341 |
while(m_scanner && ::LexerNext(m_scanner, token)) { |
| 342 |
switch(token.GetType()) { |
| 343 |
case T_PP_IF: |
| 344 |
case T_PP_IFDEF: |
| 345 |
case T_PP_IFNDEF: |
| 346 |
depth++; |
| 347 |
break; |
| 348 |
case T_PP_ENDIF: |
| 349 |
if(depth == 1) { |
| 350 |
return true; |
| 351 |
} |
| 352 |
depth--; |
| 353 |
break; |
| 354 |
case T_PP_ELIF: |
| 355 |
case T_PP_ELSE: |
| 356 |
if(depth == 1) { |
| 357 |
::LexerUnget(m_scanner); |
| 358 |
return true; |
| 359 |
} |
| 360 |
break; |
| 361 |
default: |
| 362 |
break; |
| 363 |
} |
| 364 |
} |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
void CxxPreProcessorScanner::ReadUntilMatch(int type, CxxLexerToken& token) |
| 369 |
{ |
| 370 |
while(m_scanner && ::LexerNext(m_scanner, token)) { |
| 371 |
if(token.GetType() == type) { |
| 372 |
return; |
| 373 |
} else if(token.GetType() == T_PP_STATE_EXIT) { |
| 374 |
throw CxxLexerException(wxString() << "Could not find a match for type: " << type); |
| 375 |
} |
| 376 |
} |
| 377 |
throw CxxLexerException(wxString() << "<<EOF>> Could not find a match for type: " << type); |
| 378 |
} |
| 379 |
|
| 380 |
bool CxxPreProcessorScanner::IsTokenExists(const CxxPreProcessorToken::Map_t& table, const CxxLexerToken& token) |
| 381 |
{ |
| 382 |
return table.count(token.GetWXString()); |
| 383 |
} |