Skip to content

Commit 4f6a6aa

Browse files
committed
Address review finding
1 parent 722b1e6 commit 4f6a6aa

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

tools/projmgr/include/ProjMgrUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class ProjMgrUtils {
285285
static const std::string FormatPath(const std::string& original, const std::string& directory, bool useAbsolutePaths = false);
286286

287287
/**
288-
* @brief parse semicolon-terminated variable assignments from a dbgconf file
288+
* @brief parse variable assignments from a dbgconf file
289289
* @param dbgconf path to dbgconf file
290290
* @return map of variable names to assigned values
291291
*/

tools/projmgr/src/ProjMgrUtils.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@ static constexpr const char* HIGHER_OR_EQUAL_OPERATOR = ">=";
2323
static constexpr const char* TILDE_OPERATOR = "~";
2424
static constexpr const char* CARET_OPERATOR = "^";
2525

26+
// Dbgconf assignment names follow C-style identifier rules.
2627
static bool IsDbgconfVariableName(const string& value) {
2728
if (value.empty() || (!isalpha(static_cast<unsigned char>(value[0])) && value[0] != '_')) {
2829
return false;
2930
}
3031
return all_of(value.begin() + 1, value.end(), [](unsigned char c) { return isalnum(c) || c == '_'; });
3132
}
3233

34+
// Remove surrounding whitespace without changing the assignment value content.
3335
static string Trim(const string& value) {
3436
const auto first = find_if_not(value.begin(), value.end(), [](unsigned char c) { return isspace(c); });
3537
const auto last = find_if_not(value.rbegin(), value.rend(), [](unsigned char c) { return isspace(c); }).base();
3638
return first < last ? string(first, last) : string();
3739
}
3840

41+
// Strip C and C++ comments while preserving line breaks that separate statements.
3942
static string StripCComments(const string& input) {
4043
string output;
4144
bool inLineComment = false;
@@ -500,10 +503,13 @@ map<string, string> ProjMgrUtils::ParseDbgconfFile(const string& dbgconf) {
500503
}
501504

502505
const string contentWithoutComments = StripCComments(content);
506+
// Split semicolon-separated statements, also accepting a final unterminated assignment.
503507
size_t statementStart = 0;
504508
size_t statementEnd = contentWithoutComments.find(';');
505-
while (statementEnd != string::npos) {
506-
const string statement = contentWithoutComments.substr(statementStart, statementEnd - statementStart);
509+
while (statementStart < contentWithoutComments.length()) {
510+
const string statement = contentWithoutComments.substr(
511+
statementStart,
512+
statementEnd == string::npos ? string::npos : statementEnd - statementStart);
507513
const size_t assignment = statement.find('=');
508514
if (assignment != string::npos) {
509515
const string key = Trim(statement.substr(0, assignment));
@@ -512,6 +518,9 @@ map<string, string> ProjMgrUtils::ParseDbgconfFile(const string& dbgconf) {
512518
assignments[key] = value;
513519
}
514520
}
521+
if (statementEnd == string::npos) {
522+
break;
523+
}
515524
statementStart = statementEnd + 1;
516525
statementEnd = contentWithoutComments.find(';', statementStart);
517526
}

tools/projmgr/test/src/ProjMgrUtilsUnitTests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ TEST_F(ProjMgrUtilsUnitTests, ParseDbgconfFile) {
553553
{"RoutingTPIU", "0x00000000"},
554554
{"TracePins", "(1 << 18) | 3"},
555555
{"VecResetWithPeriph", "1"},
556+
{"MissingTerminator", "2"},
556557
};
557558
EXPECT_EQ(expected, ProjMgrUtils::ParseDbgconfFile(dbgconf));
558559
EXPECT_TRUE(ProjMgrUtils::ParseDbgconfFile(testDir + "/missing.dbgconf").empty());

0 commit comments

Comments
 (0)