diff options
author | Svilen Mihaylov <svilen.mihaylov@mongodb.com> | 2023-01-06 16:27:07 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-01-06 17:30:29 +0000 |
commit | 11c922aa9199f333685c1daf598f161cb6fa56ea (patch) | |
tree | 2ed6ba2d3207c00aa2ed84192ec8868827236c9d /src/mongo/db | |
parent | 4544c162b4b60ac383d94d2ffba33f373dd2571b (diff) | |
download | mongo-11c922aa9199f333685c1daf598f161cb6fa56ea.tar.gz |
SERVER-72553 [CQF] Fix auto-updating for gcc version 12 and above
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/query/optimizer/unit_test_infra_test.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/utils/unit_test_utils.cpp | 32 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/utils/unit_test_utils.h | 2 |
3 files changed, 24 insertions, 14 deletions
diff --git a/src/mongo/db/query/optimizer/unit_test_infra_test.cpp b/src/mongo/db/query/optimizer/unit_test_infra_test.cpp index d1c2e95eb85..43ae5645bb6 100644 --- a/src/mongo/db/query/optimizer/unit_test_infra_test.cpp +++ b/src/mongo/db/query/optimizer/unit_test_infra_test.cpp @@ -101,7 +101,7 @@ TEST(TestInfra, DiffTest) { { std::ostringstream os; - outputDiff(os, expected, actual, expected.size()); + outputDiff(os, expected, actual, 0); diffStr = os.str(); } ASSERT_STR_EQ_AUTO( // NOLINT (test auto-update) @@ -127,7 +127,7 @@ TEST(TestInfra, DiffTest) { { std::ostringstream os; - outputDiff(os, {"line1\n", "line2\n", "line3\n"}, {}, 10); + outputDiff(os, {"line1\n", "line2\n", "line3\n"}, {}, 7); diffStr = os.str(); } ASSERT_STR_EQ_AUTO( // NOLINT (test auto-update) diff --git a/src/mongo/db/query/optimizer/utils/unit_test_utils.cpp b/src/mongo/db/query/optimizer/utils/unit_test_utils.cpp index e6329296f70..943e362576c 100644 --- a/src/mongo/db/query/optimizer/utils/unit_test_utils.cpp +++ b/src/mongo/db/query/optimizer/utils/unit_test_utils.cpp @@ -55,6 +55,15 @@ static constexpr const char* kTempFileSuffix = ".tmp.txt"; using LineDeltaVector = std::vector<std::pair<uint64_t, int64_t>>; std::map<std::string, LineDeltaVector> gLineDeltaMap; +// Different compilers expand __LINE__ differently. For multi-line macros, newer versions of GCC use +// the first line, while older versions use the last line. This flag accounts for the difference in +// behavior. +#if defined(__GNUC__) && (__GNUC__) >= 11 +static constexpr bool kIsFirstLine = true; +#else +static constexpr bool kIsFirstLine = false; +#endif + void maybePrintABT(const ABT& abt) { // Always print using the supported versions to make sure we don't crash. @@ -155,11 +164,9 @@ static boost::optional<size_t> diffLookAhead(const size_t thisIndex, void outputDiff(std::ostream& os, const std::vector<std::string>& expFormatted, const std::vector<std::string>& actualFormatted, - const size_t lineNumber) { + const size_t startLineNumber) { const size_t actualSize = actualFormatted.size(); const size_t expSize = expFormatted.size(); - invariant(lineNumber >= expSize); - const size_t startLineNumber = lineNumber - expSize; const auto outputLine = [&os](const bool isExpected, const size_t lineNumber, const std::string& line) { @@ -237,8 +244,13 @@ bool handleAutoUpdate(const std::string& expected, const auto expectedFormatted = formatStr(expected, needsEscaping); const auto actualFormatted = formatStr(actual, needsEscaping); - std::cout << fileName << ":" << lineNumber << ": results differ:\n"; - outputDiff(std::cout, expectedFormatted, actualFormatted, lineNumber); + // Treat an empty string as needing one line. Adjust for line delta. + const size_t expectedDelta = expectedFormatted.empty() ? 1 : expectedFormatted.size(); + // Compute the macro argument start line. + const size_t startLineNumber = kIsFirstLine ? (lineNumber + 1) : (lineNumber - expectedDelta); + + std::cout << fileName << ":" << startLineNumber << ": results differ:\n"; + outputDiff(std::cout, expectedFormatted, actualFormatted, startLineNumber); if (!mongo::unittest::getAutoUpdateOptimizerAsserts()) { std::cout << "Auto-updating is disabled.\n"; @@ -249,15 +261,13 @@ bool handleAutoUpdate(const std::string& expected, auto& lineDeltas = gLineDeltaMap.emplace(fileName, LineDeltaVector{}).first->second; int64_t totalDelta = 0; for (const auto& [line, delta] : lineDeltas) { - if (line < lineNumber) { + if (line < startLineNumber) { totalDelta += delta; } } - const size_t replacementEndLine = lineNumber + totalDelta; - // Treat an empty string as needing one line. Adjust for line delta. - const size_t expectedDelta = expectedFormatted.empty() ? 1 : expectedFormatted.size(); - const size_t replacementStartLine = replacementEndLine - expectedDelta; + const size_t replacementStartLine = startLineNumber + totalDelta; + const size_t replacementEndLine = replacementStartLine + expectedDelta; const std::string tempFileName = fileName + kTempFileSuffix; std::string line; @@ -294,7 +304,7 @@ bool handleAutoUpdate(const std::string& expected, // Add the current delta. const int64_t delta = static_cast<int64_t>(actualFormatted.size()) - expectedDelta; - lineDeltas.emplace_back(lineNumber, delta); + lineDeltas.emplace_back(startLineNumber, delta); // Do not assert in order to allow multiple tests to be updated. return true; diff --git a/src/mongo/db/query/optimizer/utils/unit_test_utils.h b/src/mongo/db/query/optimizer/utils/unit_test_utils.h index eb3b94460f4..3c782d5fdd1 100644 --- a/src/mongo/db/query/optimizer/utils/unit_test_utils.h +++ b/src/mongo/db/query/optimizer/utils/unit_test_utils.h @@ -52,7 +52,7 @@ std::string getPropsStrForExplain(const OptPhaseManager& phaseManager); void outputDiff(std::ostream& os, const std::vector<std::string>& expFormatted, const std::vector<std::string>& actualFormatted, - size_t lineNumber); + size_t startLineNumber); bool handleAutoUpdate(const std::string& expected, const std::string& actual, |