Skip to content

Commit

Permalink
Use LCOV_EXCL comments to exclude some lines from test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Feb 16, 2025
1 parent c9060c7 commit ac2abbd
Show file tree
Hide file tree
Showing 30 changed files with 133 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ CMakeCache.txt
CMakeFiles/
cmake_install.cmake
build/
*.dSYM/
callgrind.out.*
1 change: 0 additions & 1 deletion include/asm/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ Symbol *sym_RedefEqu(std::string const &symName, int32_t value);
Symbol *sym_AddVar(std::string const &symName, int32_t value);
int32_t sym_GetRSValue();
void sym_SetRSValue(int32_t value);
uint32_t sym_GetConstantValue(std::string const &symName);
// Find a symbol by exact name, bypassing expansion checks
Symbol *sym_FindExactSymbol(std::string const &symName);
// Find a symbol, possibly scoped, by name
Expand Down
2 changes: 1 addition & 1 deletion include/either.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ union Either {
} else if (other._tag == other._t2.tag_value) {
*this = other._t2.value;
} else {
_tag = nulltag;
_tag = nulltag; // LCOV_EXCL_LINE
}
return *this;
}
Expand Down
6 changes: 5 additions & 1 deletion src/asm/fstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ void fstk_SetPreIncludeFile(std::string const &path) {
warnx("Overriding pre-included filename %s", preIncludeName.c_str());
}
preIncludeName = path;
// LCOV_EXCL_START
if (verbose) {
printf("Pre-included filename %s\n", preIncludeName.c_str());
}
// LCOV_EXCL_STOP
}

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

if (!fullPath) {
if (generatedMissingIncludes && !preInclude) {
// LCOV_EXCL_START
if (verbose) {
printf("Aborting (-MG) on INCLUDE file '%s' (%s)\n", path.c_str(), strerror(errno));
}
// LCOV_EXCL_STOP
failedOnMissingInclude = true;
} else {
error("Unable to open included file '%s': %s\n", path.c_str(), strerror(errno));
Expand All @@ -319,7 +323,7 @@ void fstk_RunInclude(std::string const &path, bool preInclude) {
}

if (!newFileContext(*fullPath, false)) {
fatalerror("Failed to set up lexer for file include\n");
fatalerror("Failed to set up lexer for file include\n"); // LCOV_EXCL_LINE
}
}

Expand Down
29 changes: 24 additions & 5 deletions src/asm/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct FileUnmapDeleter {

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

Expand Down Expand Up @@ -412,21 +414,27 @@ bool LexerState::setFileAsNextState(std::string const &filePath, bool updateStat
if (filePath == "-") {
path = "<stdin>";
content.emplace<BufferedContent>(STDIN_FILENO);
// LCOV_EXCL_START
if (verbose) {
printf("Opening stdin\n");
}
// LCOV_EXCL_STOP
} else {
struct stat statBuf;
if (stat(filePath.c_str(), &statBuf) != 0) {
// LCOV_EXCL_START
error("Failed to stat file \"%s\": %s\n", filePath.c_str(), strerror(errno));
return false;
// LCOV_EXCL_STOP
}
path = filePath;

int fd = open(path.c_str(), O_RDONLY);
if (fd < 0) {
// LCOV_EXCL_START
error("Failed to open file \"%s\": %s\n", path.c_str(), strerror(errno));
return false;
// LCOV_EXCL_STOP
}

bool isMmapped = false;
Expand All @@ -438,16 +446,19 @@ bool LexerState::setFileAsNextState(std::string const &filePath, bool updateStat
content.emplace<ViewedContent>(
std::shared_ptr<char[]>(mappingAddr, FileUnmapDeleter(size)), size
);
// LCOV_EXCL_START
if (verbose) {
printf("File \"%s\" is mmap()ped\n", path.c_str());
}
// LCOV_EXCL_STOP
isMmapped = true;
}
}

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

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

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

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

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

case T_(POP_ENDR):
depth--;
if (!depth) {
return Token(T_(YYEOF)); // yywrap() will finish the REPT/FOR loop
}
// `lexer_CaptureRept` has already guaranteed that the `ENDR`s are balanced
assume(depth > 0);
break;

case T_(POP_IF):
Expand Down
14 changes: 12 additions & 2 deletions src/asm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ static option const longopts[] = {
{nullptr, no_argument, nullptr, 0 }
};

// LCOV_EXCL_START
static void printUsage() {
fputs(
"Usage: rgbasm [-EhVvw] [-b chars] [-D name[=value]] [-g chars] [-I path]\n"
Expand All @@ -107,6 +108,7 @@ static void printUsage() {
stderr
);
}
// LCOV_EXCL_STOP

int main(int argc, char *argv[]) {
time_t now = time(nullptr);
Expand Down Expand Up @@ -176,8 +178,10 @@ int main(int argc, char *argv[]) {
break;

case 'h':
// LCOV_EXCL_START
printUsage();
exit(0);
// LCOV_EXCL_STOP

case 'I':
fstk_AddIncludePath(musl_optarg);
Expand All @@ -195,7 +199,7 @@ int main(int argc, char *argv[]) {
dependFileName = "<stdout>";
}
if (dependFile == nullptr) {
err("Failed to open dependfile \"%s\"", dependFileName);
err("Failed to open dependfile \"%s\"", dependFileName); // LCOV_EXCL_LINE
}
break;

Expand Down Expand Up @@ -302,9 +306,11 @@ int main(int argc, char *argv[]) {
if (stateFileSpecs.find(name) != stateFileSpecs.end()) {
warnx("Overriding state filename %s", name);
}
// LCOV_EXCL_START
if (verbose) {
printf("State filename %s\n", name);
}
// LCOV_EXCL_STOP
stateFileSpecs.emplace(name, std::move(features));
break;
}
Expand All @@ -314,8 +320,10 @@ int main(int argc, char *argv[]) {
exit(0);

case 'v':
// LCOV_EXCL_START
verbose = true;
break;
// LCOV_EXCL_STOP

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

// Unrecognized options
default:
// LCOV_EXCL_START
printUsage();
exit(1);
// LCOV_EXCL_STOP
}
}

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

if (verbose) {
printf("Assembling %s\n", mainFileName.c_str());
printf("Assembling %s\n", mainFileName.c_str()); // LCOV_EXCL_LINE
}

if (dependFile) {
Expand Down
22 changes: 9 additions & 13 deletions src/asm/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void out_RegisterNode(std::shared_ptr<FileStackNode> node) {
}
}

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

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

static void writePatch(Patch const &patch, FILE *file) {
Expand Down Expand Up @@ -175,7 +176,7 @@ static void writeRpn(std::vector<uint8_t> &rpnexpr, std::vector<uint8_t> const &
sym = sym_FindExactSymbol(symName);
if (sym->isConstant()) {
rpnexpr[rpnptr++] = RPN_CONST;
value = sym_GetConstantValue(symName);
value = sym->getConstantValue();
} else {
rpnexpr[rpnptr++] = RPN_SYM;
registerUnregisteredSymbol(*sym); // Ensure that `sym->ID` is set
Expand Down Expand Up @@ -323,7 +324,7 @@ void out_WriteObject() {
file = stdout;
}
if (!file) {
err("Failed to open object file '%s'", objectFileName.c_str());
err("Failed to open object file '%s'", objectFileName.c_str()); // LCOV_EXCL_LINE
}
Defer closeFile{[&] { fclose(file); }};

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

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

for (Symbol const *sym : objectSymbols) {
Expand All @@ -373,9 +367,11 @@ void out_SetFileName(std::string const &name) {
warnx("Overriding output filename %s", objectFileName.c_str());
}
objectFileName = name;
// LCOV_EXCL_START
if (verbose) {
printf("Output filename %s\n", objectFileName.c_str());
}
// LCOV_EXCL_STOP
}

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

Expand Down
Loading

0 comments on commit ac2abbd

Please sign in to comment.