Skip to content

Commit 6c61400

Browse files
committed
#174 remove line markers from output using -P on the command line
1 parent 2499b51 commit 6c61400

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎main.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ int main(int argc, char **argv)
153153
found = true;
154154
}
155155
break;
156+
case 'P':
157+
if (std::strcmp(arg, "-P")==0) {
158+
dui.ignoreLineDirectives = true;
159+
found = true;
160+
}
161+
break;
156162
}
157163
if (!found) {
158164
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
@@ -187,6 +193,7 @@ int main(int argc, char **argv)
187193
std::cout << " -e Output errors only." << std::endl;
188194
std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
189195
std::cout << " -l Print lines numbers." << std::endl;
196+
std::cout << " -P Ignore #line / # <num> \"file\" directives (do not let them affect location tracking)." << std::endl;
190197
return 0;
191198
}
192199

‎simplecpp.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,8 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
799799
else
800800
fileindex = location.fileIndex;
801801

802-
lineDirective(fileindex, line, location);
802+
if (!dui.ignoreLineDirectives)
803+
lineDirective(fileindex, line, location);
803804
}
804805

805806
continue;
@@ -3223,7 +3224,7 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat
32233224
return {id_it->second, false};
32243225
}
32253226

3226-
auto *const data = new FileData {path, TokenList(path, filenames, {}, outputList)};
3227+
auto *const data = new FileData {path, TokenList(path, filenames, dui, outputList)};
32273228

32283229
if (dui.removeComments)
32293230
data->tokens.removeComments();

‎simplecpp.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ namespace simplecpp {
265265

266266
/**
267267
* Command line preprocessor settings.
268-
* On the command line these are configured by -D, -U, -I, --include, -std
268+
* On the command line these are configured by -D, -U, -I, --include, -std, -P
269269
*/
270270
struct SIMPLECPP_LIB DUI {
271271
DUI() = default;
@@ -276,6 +276,7 @@ namespace simplecpp {
276276
std::string std;
277277
bool clearIncludeCache{};
278278
bool removeComments{}; /** remove comment tokens from included files */
279+
bool ignoreLineDirectives{}; /** -P: do not let #line directives affect location tracking */
279280
};
280281

281282
/** List of tokens. */

‎test.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,6 +2731,19 @@ static void location12()
27312731
}
27322732

27332733

2734+
static void location13()
2735+
{
2736+
simplecpp::DUI dui;
2737+
dui.ignoreLineDirectives = true;
2738+
2739+
const char code[] =
2740+
"#line 3 \"file.c\"\n"
2741+
"__LINE__ __FILE__\n";
2742+
ASSERT_EQUALS("\n"
2743+
"2 \"\"",
2744+
preprocess(code, dui));
2745+
}
2746+
27342747
static void missingHeader1()
27352748
{
27362749
const char code[] = "#include \"notexist.h\"\n";
@@ -4750,6 +4763,7 @@ static void runTests(int argc, char **argv, Input input)
47504763
TEST_CASE(location10);
47514764
TEST_CASE(location11);
47524765
TEST_CASE(location12);
4766+
TEST_CASE(location13);
47534767

47544768
TEST_CASE(missingHeader1);
47554769
TEST_CASE(missingHeader2);

0 commit comments

Comments
 (0)