Skip to content

Commit 632342b

Browse files
authored
Use LCOV_EXCL comments to exclude some lines from test coverage (#1662)
1 parent c9060c7 commit 632342b

30 files changed

+133
-100
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ CMakeCache.txt
1414
CMakeFiles/
1515
cmake_install.cmake
1616
build/
17+
*.dSYM/
1718
callgrind.out.*

include/asm/symbol.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ Symbol *sym_RedefEqu(std::string const &symName, int32_t value);
8282
Symbol *sym_AddVar(std::string const &symName, int32_t value);
8383
int32_t sym_GetRSValue();
8484
void sym_SetRSValue(int32_t value);
85-
uint32_t sym_GetConstantValue(std::string const &symName);
8685
// Find a symbol by exact name, bypassing expansion checks
8786
Symbol *sym_FindExactSymbol(std::string const &symName);
8887
// Find a symbol, possibly scoped, by name

include/either.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ union Either {
103103
} else if (other._tag == other._t2.tag_value) {
104104
*this = other._t2.value;
105105
} else {
106-
_tag = nulltag;
106+
_tag = nulltag; // LCOV_EXCL_LINE
107107
}
108108
return *this;
109109
}

src/asm/fstack.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ void fstk_SetPreIncludeFile(std::string const &path) {
119119
warnx("Overriding pre-included filename %s", preIncludeName.c_str());
120120
}
121121
preIncludeName = path;
122+
// LCOV_EXCL_START
122123
if (verbose) {
123124
printf("Pre-included filename %s\n", preIncludeName.c_str());
124125
}
126+
// LCOV_EXCL_STOP
125127
}
126128

127129
static void printDep(std::string const &path) {
@@ -308,9 +310,11 @@ void fstk_RunInclude(std::string const &path, bool preInclude) {
308310

309311
if (!fullPath) {
310312
if (generatedMissingIncludes && !preInclude) {
313+
// LCOV_EXCL_START
311314
if (verbose) {
312315
printf("Aborting (-MG) on INCLUDE file '%s' (%s)\n", path.c_str(), strerror(errno));
313316
}
317+
// LCOV_EXCL_STOP
314318
failedOnMissingInclude = true;
315319
} else {
316320
error("Unable to open included file '%s': %s\n", path.c_str(), strerror(errno));
@@ -319,7 +323,7 @@ void fstk_RunInclude(std::string const &path, bool preInclude) {
319323
}
320324

321325
if (!newFileContext(*fullPath, false)) {
322-
fatalerror("Failed to set up lexer for file include\n");
326+
fatalerror("Failed to set up lexer for file include\n"); // LCOV_EXCL_LINE
323327
}
324328
}
325329

src/asm/lexer.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct FileUnmapDeleter {
7777

7878
static char *mapFile(int fd, std::string const &path, size_t size) {
7979
void *mappingAddr = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
80+
// LCOV_EXCL_START
8081
if (mappingAddr == MAP_FAILED && errno == ENOTSUP) {
8182
// The implementation may not support MAP_PRIVATE; try again with MAP_SHARED
8283
// instead, offering, I believe, weaker guarantees about external modifications to
@@ -86,6 +87,7 @@ static char *mapFile(int fd, std::string const &path, size_t size) {
8687
}
8788
mappingAddr = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
8889
}
90+
// LCOV_EXCL_STOP
8991
return mappingAddr != MAP_FAILED ? static_cast<char *>(mappingAddr) : nullptr;
9092
}
9193

@@ -412,21 +414,27 @@ bool LexerState::setFileAsNextState(std::string const &filePath, bool updateStat
412414
if (filePath == "-") {
413415
path = "<stdin>";
414416
content.emplace<BufferedContent>(STDIN_FILENO);
417+
// LCOV_EXCL_START
415418
if (verbose) {
416419
printf("Opening stdin\n");
417420
}
421+
// LCOV_EXCL_STOP
418422
} else {
419423
struct stat statBuf;
420424
if (stat(filePath.c_str(), &statBuf) != 0) {
425+
// LCOV_EXCL_START
421426
error("Failed to stat file \"%s\": %s\n", filePath.c_str(), strerror(errno));
422427
return false;
428+
// LCOV_EXCL_STOP
423429
}
424430
path = filePath;
425431

426432
int fd = open(path.c_str(), O_RDONLY);
427433
if (fd < 0) {
434+
// LCOV_EXCL_START
428435
error("Failed to open file \"%s\": %s\n", path.c_str(), strerror(errno));
429436
return false;
437+
// LCOV_EXCL_STOP
430438
}
431439

432440
bool isMmapped = false;
@@ -438,16 +446,19 @@ bool LexerState::setFileAsNextState(std::string const &filePath, bool updateStat
438446
content.emplace<ViewedContent>(
439447
std::shared_ptr<char[]>(mappingAddr, FileUnmapDeleter(size)), size
440448
);
449+
// LCOV_EXCL_START
441450
if (verbose) {
442451
printf("File \"%s\" is mmap()ped\n", path.c_str());
443452
}
453+
// LCOV_EXCL_STOP
444454
isMmapped = true;
445455
}
446456
}
447457

448458
if (!isMmapped) {
449459
// Sometimes mmap() fails or isn't available, so have a fallback
450460
content.emplace<BufferedContent>(fd);
461+
// LCOV_EXCL_START
451462
if (verbose) {
452463
if (statBuf.st_size == 0) {
453464
printf("File \"%s\" is empty\n", path.c_str());
@@ -457,6 +468,7 @@ bool LexerState::setFileAsNextState(std::string const &filePath, bool updateStat
457468
);
458469
}
459470
}
471+
// LCOV_EXCL_STOP
460472
}
461473
}
462474

@@ -552,7 +564,9 @@ size_t BufferedContent::readMore(size_t startIndex, size_t nbChars) {
552564
ssize_t nbReadChars = read(fd, &buf[startIndex], nbChars);
553565

554566
if (nbReadChars == -1) {
567+
// LCOV_EXCL_START
555568
fatalerror("Error while reading \"%s\": %s\n", lexerState->path.c_str(), strerror(errno));
569+
// LCOV_EXCL_STOP
556570
}
557571

558572
size += nbReadChars;
@@ -761,7 +775,9 @@ int LexerState::peekCharAhead() {
761775
// and `.peekCharAhead()` will continue with its parent
762776
assume(exp.offset <= exp.size());
763777
if (exp.offset + distance < exp.size()) {
764-
return static_cast<uint8_t>((*exp.contents)[exp.offset + distance]);
778+
// Macro args can't be recursive, since `peek()` marks them as scanned, so
779+
// this is a failsafe that (as far as I can tell) won't ever actually run.
780+
return static_cast<uint8_t>((*exp.contents)[exp.offset + distance]); // LCOV_EXCL_LINE
765781
}
766782
distance -= exp.size() - exp.offset;
767783
}
@@ -1323,8 +1339,11 @@ static void appendEscapedString(std::string &str, std::string const &escape) {
13231339
str += "\\n";
13241340
break;
13251341
case '\r':
1342+
// A literal CR in a string may get treated as a LF, so '\r' is not tested.
1343+
// LCOV_EXCL_START
13261344
str += "\\r";
13271345
break;
1346+
// LCOV_EXCL_STOP
13281347
case '\t':
13291348
str += "\\t";
13301349
break;
@@ -2163,7 +2182,8 @@ static Token skipIfBlock(bool toEndc) {
21632182

21642183
case T_(POP_ELIF):
21652184
if (lexer_ReachedELSEBlock()) {
2166-
fatalerror("Found ELIF after an ELSE block\n");
2185+
// This should be redundant, as the parser handles this error first.
2186+
fatalerror("Found ELIF after an ELSE block\n"); // LCOV_EXCL_LINE
21672187
}
21682188
if (!toEndc && lexer_GetIFDepth() == startingDepth) {
21692189
return token;
@@ -2255,9 +2275,8 @@ static Token yylex_SKIP_TO_ENDR() {
22552275

22562276
case T_(POP_ENDR):
22572277
depth--;
2258-
if (!depth) {
2259-
return Token(T_(YYEOF)); // yywrap() will finish the REPT/FOR loop
2260-
}
2278+
// `lexer_CaptureRept` has already guaranteed that the `ENDR`s are balanced
2279+
assume(depth > 0);
22612280
break;
22622281

22632282
case T_(POP_IF):

src/asm/main.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ static option const longopts[] = {
8787
{nullptr, no_argument, nullptr, 0 }
8888
};
8989

90+
// LCOV_EXCL_START
9091
static void printUsage() {
9192
fputs(
9293
"Usage: rgbasm [-EhVvw] [-b chars] [-D name[=value]] [-g chars] [-I path]\n"
@@ -107,6 +108,7 @@ static void printUsage() {
107108
stderr
108109
);
109110
}
111+
// LCOV_EXCL_STOP
110112

111113
int main(int argc, char *argv[]) {
112114
time_t now = time(nullptr);
@@ -176,8 +178,10 @@ int main(int argc, char *argv[]) {
176178
break;
177179

178180
case 'h':
181+
// LCOV_EXCL_START
179182
printUsage();
180183
exit(0);
184+
// LCOV_EXCL_STOP
181185

182186
case 'I':
183187
fstk_AddIncludePath(musl_optarg);
@@ -195,7 +199,7 @@ int main(int argc, char *argv[]) {
195199
dependFileName = "<stdout>";
196200
}
197201
if (dependFile == nullptr) {
198-
err("Failed to open dependfile \"%s\"", dependFileName);
202+
err("Failed to open dependfile \"%s\"", dependFileName); // LCOV_EXCL_LINE
199203
}
200204
break;
201205

@@ -302,9 +306,11 @@ int main(int argc, char *argv[]) {
302306
if (stateFileSpecs.find(name) != stateFileSpecs.end()) {
303307
warnx("Overriding state filename %s", name);
304308
}
309+
// LCOV_EXCL_START
305310
if (verbose) {
306311
printf("State filename %s\n", name);
307312
}
313+
// LCOV_EXCL_STOP
308314
stateFileSpecs.emplace(name, std::move(features));
309315
break;
310316
}
@@ -314,8 +320,10 @@ int main(int argc, char *argv[]) {
314320
exit(0);
315321

316322
case 'v':
323+
// LCOV_EXCL_START
317324
verbose = true;
318325
break;
326+
// LCOV_EXCL_STOP
319327

320328
case 'W':
321329
opt_W(musl_optarg);
@@ -367,8 +375,10 @@ int main(int argc, char *argv[]) {
367375

368376
// Unrecognized options
369377
default:
378+
// LCOV_EXCL_START
370379
printUsage();
371380
exit(1);
381+
// LCOV_EXCL_STOP
372382
}
373383
}
374384

@@ -391,7 +401,7 @@ int main(int argc, char *argv[]) {
391401
std::string mainFileName = argv[musl_optind];
392402

393403
if (verbose) {
394-
printf("Assembling %s\n", mainFileName.c_str());
404+
printf("Assembling %s\n", mainFileName.c_str()); // LCOV_EXCL_LINE
395405
}
396406

397407
if (dependFile) {

src/asm/output.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void out_RegisterNode(std::shared_ptr<FileStackNode> node) {
6161
}
6262
}
6363

64-
// Return a section's ID, or UINT32_MAX if the section is not in the list
64+
// Return a section's ID, or UINT32_MAX if the section does not exist
6565
static uint32_t getSectIDIfAny(Section *sect) {
6666
if (!sect) {
6767
return UINT32_MAX;
@@ -71,7 +71,8 @@ static uint32_t getSectIDIfAny(Section *sect) {
7171
return static_cast<uint32_t>(search->second);
7272
}
7373

74-
fatalerror("Unknown section '%s'\n", sect->name.c_str());
74+
// Every section that exists should be in `sectionMap`
75+
fatalerror("Unknown section '%s'\n", sect->name.c_str()); // LCOV_EXCL_LINE
7576
}
7677

7778
static void writePatch(Patch const &patch, FILE *file) {
@@ -175,7 +176,7 @@ static void writeRpn(std::vector<uint8_t> &rpnexpr, std::vector<uint8_t> const &
175176
sym = sym_FindExactSymbol(symName);
176177
if (sym->isConstant()) {
177178
rpnexpr[rpnptr++] = RPN_CONST;
178-
value = sym_GetConstantValue(symName);
179+
value = sym->getConstantValue();
179180
} else {
180181
rpnexpr[rpnptr++] = RPN_SYM;
181182
registerUnregisteredSymbol(*sym); // Ensure that `sym->ID` is set
@@ -323,7 +324,7 @@ void out_WriteObject() {
323324
file = stdout;
324325
}
325326
if (!file) {
326-
err("Failed to open object file '%s'", objectFileName.c_str());
327+
err("Failed to open object file '%s'", objectFileName.c_str()); // LCOV_EXCL_LINE
327328
}
328329
Defer closeFile{[&] { fclose(file); }};
329330

@@ -343,14 +344,7 @@ void out_WriteObject() {
343344
writeFileStackNode(node, file);
344345

345346
// The list is supposed to have decrementing IDs
346-
if (it + 1 != fileStackNodes.end() && it[1]->ID != node.ID - 1) {
347-
fatalerror(
348-
"Internal error: fstack node #%" PRIu32 " follows #%" PRIu32
349-
". Please report this to the developers!\n",
350-
it[1]->ID,
351-
node.ID
352-
);
353-
}
347+
assume(it + 1 == fileStackNodes.end() || it[1]->ID == node.ID - 1);
354348
}
355349

356350
for (Symbol const *sym : objectSymbols) {
@@ -373,9 +367,11 @@ void out_SetFileName(std::string const &name) {
373367
warnx("Overriding output filename %s", objectFileName.c_str());
374368
}
375369
objectFileName = name;
370+
// LCOV_EXCL_START
376371
if (verbose) {
377372
printf("Output filename %s\n", objectFileName.c_str());
378373
}
374+
// LCOV_EXCL_STOP
379375
}
380376

381377
static void dumpString(std::string const &escape, FILE *file) {
@@ -528,7 +524,7 @@ void out_WriteState(std::string name, std::vector<StateFeature> const &features)
528524
file = stdout;
529525
}
530526
if (!file) {
531-
err("Failed to open state file '%s'", name.c_str());
527+
err("Failed to open state file '%s'", name.c_str()); // LCOV_EXCL_LINE
532528
}
533529
Defer closeFile{[&] { fclose(file); }};
534530

0 commit comments

Comments
 (0)