| 471 | } |
| 472 | |
| 473 | CBMPreprocessedSource *cbm_preprocess_with_map(const char *source, int source_len, |
| 474 | const char *filename, const char **extra_defines, |
| 475 | const char **include_paths, int cpp_mode) { |
| 476 | // Run the second pass when there are directives to evaluate OR when the file |
| 477 | // carries export-macro-shaped identifiers to predefine empty (#1989) — a UE |
| 478 | // plugin header with only `#pragma once` + `#include` lines still needs it. |
| 479 | if (!has_preprocessor_work(source, source_len) && |
| 480 | !has_export_macro_candidates(source, source_len)) { |
| 481 | return NULL; // NULL = no expansion needed, use original |
| 482 | } |
| 483 | |
| 484 | try { |
| 485 | simplecpp::DUI dui; |
| 486 | if (extra_defines) { |
| 487 | for (int i = 0; extra_defines[i]; i++) |
| 488 | dui.defines.push_back(extra_defines[i]); |
| 489 | } |
| 490 | // Predefine collected export-macro candidates as empty, mirroring the |
| 491 | // real compile command line (`/D "MODULE_API="`). Names already provided |
| 492 | // by the caller win — never override an explicit define. |
| 493 | char export_cands[CBM_EXPORT_MACRO_MAX][CBM_EXPORT_MACRO_NAME_MAX]; |
| 494 | int export_cand_count = |
| 495 | collect_export_macro_candidates(source, source_len, export_cands, CBM_EXPORT_MACRO_MAX); |
| 496 | for (int i = 0; i < export_cand_count; i++) { |
| 497 | bool provided = false; |
| 498 | for (std::list<std::string>::const_iterator it = dui.defines.begin(); |
| 499 | it != dui.defines.end(); ++it) { |
| 500 | const std::string &def = *it; |
| 501 | size_t eq = def.find('='); |
| 502 | std::string name = (eq == std::string::npos) ? def : def.substr(0, eq); |
| 503 | if (name == export_cands[i]) { |
| 504 | provided = true; |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | if (!provided) { |
| 509 | dui.defines.push_back(std::string(export_cands[i]) + "="); |
| 510 | } |
| 511 | } |
| 512 | if (include_paths) { |
| 513 | for (int i = 0; include_paths[i]; i++) |
| 514 | dui.includePaths.push_back(include_paths[i]); |
| 515 | } |
| 516 | dui.std = cpp_mode ? "c++17" : "c11"; |
| 517 | |
| 518 | std::string src(source, source_len); |
| 519 | std::istringstream istr(src); |
| 520 | std::vector<std::string> files; |
| 521 | files.push_back(filename ? filename : "<input>"); |
| 522 | |
| 523 | simplecpp::TokenList rawtokens(istr, files, files[0]); |
| 524 | simplecpp::TokenList output(files); |
| 525 | simplecpp::FileDataCache filedata = simplecpp::load(rawtokens, files, dui); |
| 526 | |
| 527 | simplecpp::preprocess(output, rawtokens, files, filedata, dui); |
| 528 | |
| 529 | std::string result = stringify_source_spacing(output); |
| 530 |
no test coverage detected