Skip to content

Commit dcc4fdd

Browse files
authored
main.cpp: error out when file/path provided by -I or -include=, or the input does not exist (#435)
1 parent dcc0380 commit dcc4fdd

File tree

2 files changed

+202
-21
lines changed

2 files changed

+202
-21
lines changed

integration_test.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,151 @@ def test_pragma_once_matching(record_property, tmpdir):
298298

299299
assert stdout.count("ONCE") == 1
300300
assert stderr == ""
301+
302+
303+
def test_input_multiple(record_property, tmpdir):
304+
test_file = os.path.join(tmpdir, "test.c")
305+
with open(test_file, 'w'):
306+
pass
307+
308+
test_file_1 = os.path.join(tmpdir, "test1.c")
309+
with open(test_file_1, 'w'):
310+
pass
311+
312+
args = [
313+
'test.c',
314+
'test1.c'
315+
]
316+
317+
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
318+
record_property("stdout", stdout)
319+
record_property("stderr", stderr)
320+
321+
assert '' == stderr
322+
assert "error: multiple filenames specified\n" == stdout
323+
324+
325+
def test_input_missing(record_property, tmpdir):
326+
args = [
327+
'missing.c'
328+
]
329+
330+
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
331+
record_property("stdout", stdout)
332+
record_property("stderr", stderr)
333+
334+
assert '' == stderr
335+
assert "error: could not open file 'missing.c'\n" == stdout
336+
337+
338+
def test_input_dir(record_property, tmpdir):
339+
test_dir = os.path.join(tmpdir, "test")
340+
os.mkdir(test_dir)
341+
342+
args = [
343+
'test'
344+
]
345+
346+
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
347+
record_property("stdout", stdout)
348+
record_property("stderr", stderr)
349+
350+
assert '' == stderr
351+
assert "error: could not open file 'test'\n" == stdout
352+
353+
354+
def test_incpath_missing(record_property, tmpdir):
355+
test_file = os.path.join(tmpdir, "test.c")
356+
with open(test_file, 'w'):
357+
pass
358+
359+
test_dir = os.path.join(tmpdir, "test")
360+
os.mkdir(test_dir)
361+
362+
args = [
363+
'-Itest',
364+
'-Imissing',
365+
'test.c'
366+
]
367+
368+
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
369+
record_property("stdout", stdout)
370+
record_property("stderr", stderr)
371+
372+
assert '' == stderr
373+
assert "error: could not find include path 'missing'\n" == stdout
374+
375+
376+
def test_incpath_file(record_property, tmpdir):
377+
test_file = os.path.join(tmpdir, "test.c")
378+
with open(test_file, 'w'):
379+
pass
380+
381+
inc_dir = os.path.join(tmpdir, "inc")
382+
os.mkdir(inc_dir)
383+
384+
inc_file = os.path.join(tmpdir, "inc.h")
385+
with open(test_file, 'w'):
386+
pass
387+
388+
args = [
389+
'-Iinc',
390+
'-Iinc.h',
391+
'test.c'
392+
]
393+
394+
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
395+
record_property("stdout", stdout)
396+
record_property("stderr", stderr)
397+
398+
assert '' == stderr
399+
assert "error: could not find include path 'inc.h'\n" == stdout
400+
401+
402+
def test_incfile_missing(record_property, tmpdir):
403+
test_file = os.path.join(tmpdir, "test.c")
404+
with open(test_file, 'w'):
405+
pass
406+
407+
inc_file = os.path.join(tmpdir, "inc.h")
408+
with open(inc_file, 'w'):
409+
pass
410+
411+
args = [
412+
'-include=inc.h',
413+
'-include=missing.h',
414+
'test.c'
415+
]
416+
417+
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
418+
record_property("stdout", stdout)
419+
record_property("stderr", stderr)
420+
421+
assert '' == stderr
422+
assert "error: could not open include 'missing.h'\n" == stdout
423+
424+
425+
def test_incpath_dir(record_property, tmpdir):
426+
test_file = os.path.join(tmpdir, "test.c")
427+
with open(test_file, 'w'):
428+
pass
429+
430+
inc_file = os.path.join(tmpdir, "inc.h")
431+
with open(inc_file, 'w'):
432+
pass
433+
434+
inc_dir = os.path.join(tmpdir, "inc")
435+
os.mkdir(inc_dir)
436+
437+
args = [
438+
'-include=inc.h',
439+
'-include=inc',
440+
'test.c'
441+
]
442+
443+
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
444+
record_property("stdout", stdout)
445+
record_property("stderr", stderr)
446+
447+
assert '' == stderr
448+
assert "error: could not open include 'inc'\n" == stdout

main.cpp

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@
99
#include <cstring>
1010
#include <fstream>
1111
#include <iostream>
12+
#include <sys/stat.h>
1213
#include <string>
1314
#include <utility>
1415
#include <vector>
1516

17+
static bool isDir(const std::string& path)
18+
{
19+
struct stat file_stat;
20+
if (stat(path.c_str(), &file_stat) == -1)
21+
return false;
22+
23+
return (file_stat.st_mode & S_IFMT) == S_IFDIR;
24+
}
25+
1626
int main(int argc, char **argv)
1727
{
1828
bool error = false;
@@ -23,6 +33,7 @@ int main(int argc, char **argv)
2333

2434
// Settings..
2535
simplecpp::DUI dui;
36+
dui.removeComments = true;
2637
bool quiet = false;
2738
bool error_only = false;
2839
for (int i = 1; i < argc; i++) {
@@ -114,18 +125,18 @@ int main(int argc, char **argv)
114125
}
115126
} else if (filename) {
116127
std::cout << "error: multiple filenames specified" << std::endl;
117-
std::exit(1);
128+
return 1;
118129
} else {
119130
filename = arg;
120131
}
121132
}
122133

123134
if (error)
124-
std::exit(1);
135+
return 1;
125136

126137
if (quiet && error_only) {
127138
std::cout << "error: -e cannot be used in conjunction with -q" << std::endl;
128-
std::exit(1);
139+
return 1;
129140
}
130141

131142
if (!filename) {
@@ -141,32 +152,54 @@ int main(int argc, char **argv)
141152
std::cout << " -e Output errors only." << std::endl;
142153
std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
143154
std::cout << " -l Print lines numbers." << std::endl;
144-
std::exit(0);
155+
return 0;
145156
}
146157

147-
dui.removeComments = true;
158+
// TODO: move this logic into simplecpp
159+
bool inp_missing = false;
160+
161+
for (const std::string& inc : dui.includes) {
162+
std::ifstream f(inc);
163+
if (!f.is_open() || isDir(inc)) {
164+
inp_missing = true;
165+
std::cout << "error: could not open include '" << inc << "'" << std::endl;
166+
}
167+
}
168+
169+
for (const std::string& inc : dui.includePaths) {
170+
if (!isDir(inc)) {
171+
inp_missing = true;
172+
std::cout << "error: could not find include path '" << inc << "'" << std::endl;
173+
}
174+
}
175+
176+
std::ifstream f(filename);
177+
if (!f.is_open() || isDir(filename)) {
178+
inp_missing = true;
179+
std::cout << "error: could not open file '" << filename << "'" << std::endl;
180+
}
181+
182+
if (inp_missing)
183+
return 1;
148184

149185
// Perform preprocessing
150186
simplecpp::OutputList outputList;
151187
std::vector<std::string> files;
152-
simplecpp::TokenList *rawtokens;
153-
if (use_istream) {
154-
std::ifstream f(filename);
155-
if (!f.is_open()) {
156-
std::cout << "error: could not open file '" << filename << "'" << std::endl;
157-
std::exit(1);
188+
simplecpp::TokenList outputTokens(files);
189+
{
190+
simplecpp::TokenList *rawtokens;
191+
if (use_istream) {
192+
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
193+
} else {
194+
f.close();
195+
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
158196
}
159-
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
160-
} else {
161-
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
197+
rawtokens->removeComments();
198+
simplecpp::FileDataCache filedata;
199+
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
200+
simplecpp::cleanup(filedata);
201+
delete rawtokens;
162202
}
163-
rawtokens->removeComments();
164-
simplecpp::TokenList outputTokens(files);
165-
simplecpp::FileDataCache filedata;
166-
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
167-
simplecpp::cleanup(filedata);
168-
delete rawtokens;
169-
rawtokens = nullptr;
170203

171204
// Output
172205
if (!quiet) {

0 commit comments

Comments
 (0)