diff --git a/main.cpp b/main.cpp index 92ca27dc..f44d63bc 100644 --- a/main.cpp +++ b/main.cpp @@ -153,6 +153,12 @@ int main(int argc, char **argv) found = true; } break; + case 'P': + if (std::strcmp(arg, "-P")==0) { + dui.ignoreLineDirectives = true; + found = true; + } + break; } if (!found) { std::cout << "error: option '" << arg << "' is unknown." << std::endl; @@ -187,6 +193,7 @@ int main(int argc, char **argv) std::cout << " -e Output errors only." << std::endl; std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl; std::cout << " -l Print lines numbers." << std::endl; + std::cout << " -P Ignore #line / # \"file\" directives (do not let them affect location tracking)." << std::endl; return 0; } diff --git a/simplecpp.cpp b/simplecpp.cpp index 8237ac75..6c7e6267 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -799,7 +799,8 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, else fileindex = location.fileIndex; - lineDirective(fileindex, line, location); + if (!dui.ignoreLineDirectives) + lineDirective(fileindex, line, location); } continue; @@ -3223,7 +3224,7 @@ std::pair simplecpp::FileDataCache::tryload(FileDat return {id_it->second, false}; } - auto *const data = new FileData {path, TokenList(path, filenames, {}, outputList)}; + auto *const data = new FileData {path, TokenList(path, filenames, dui, outputList)}; if (dui.removeComments) data->tokens.removeComments(); diff --git a/simplecpp.h b/simplecpp.h index f4dca183..1fa9bfca 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -265,7 +265,7 @@ namespace simplecpp { /** * Command line preprocessor settings. - * On the command line these are configured by -D, -U, -I, --include, -std + * On the command line these are configured by -D, -U, -I, --include, -std, -P */ struct SIMPLECPP_LIB DUI { DUI() = default; @@ -276,6 +276,7 @@ namespace simplecpp { std::string std; bool clearIncludeCache{}; bool removeComments{}; /** remove comment tokens from included files */ + bool ignoreLineDirectives{}; /** -P: do not let #line directives affect location tracking */ }; /** List of tokens. */ diff --git a/test.cpp b/test.cpp index 2b0e8ee0..b605aebe 100644 --- a/test.cpp +++ b/test.cpp @@ -2731,6 +2731,19 @@ static void location12() } +static void location13() +{ + simplecpp::DUI dui; + dui.ignoreLineDirectives = true; + + const char code[] = + "#line 3 \"file.c\"\n" + "__LINE__ __FILE__\n"; + ASSERT_EQUALS("\n" + "2 \"\"", + preprocess(code, dui)); +} + static void missingHeader1() { const char code[] = "#include \"notexist.h\"\n"; @@ -4750,6 +4763,7 @@ static void runTests(int argc, char **argv, Input input) TEST_CASE(location10); TEST_CASE(location11); TEST_CASE(location12); + TEST_CASE(location13); TEST_CASE(missingHeader1); TEST_CASE(missingHeader2);