-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang] Fix sorting header paths #73323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This code was initially written in commit 7ff2914 with the intention of sorting headers according to their path. At the time, the path was saved in field NameAsWritten of Module::Header. Later, commit e6830b6 added field PathRelativeToRootModuleDirectory to Module::Header and modified ModuleMapParser::parseUmbrellaDirDecl() so that it started to save the header path in the new field and started setting NameAsWritten = "". It didn't modify compareModuleHeaders() in order to adapt it to the new field. After this commit, the sorting stopped working because it continued comparing only NameAsWritten. This commit fixes it by treating NameAsWritten and PathRelativeToRootModuleDirectory as a tuple when comparing Module::Header.
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: Tulio Magno Quites Machado Filho (tuliom) ChangesThis code was initially written in commit Later, commit e6830b6 added field PathRelativeToRootModuleDirectory to Module::Header and modified ModuleMapParser::parseUmbrellaDirDecl() so that it started to save the header path in the new field and started setting NameAsWritten = "". It didn't modify compareModuleHeaders() in order to adapt it to the new field. After this commit, the sorting stopped working because it continued comparing only NameAsWritten. This commit fixes it by treating NameAsWritten and PathRelativeToRootModuleDirectory as a tuple when comparing Module::Header. Full diff: https://github.com/llvm/llvm-project/pull/73323.diff 1 Files Affected:
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index 1d67e275cb4775a..7bc89b2fed36bf2 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -49,6 +49,7 @@
#include <optional>
#include <string>
#include <system_error>
+#include <tuple>
#include <utility>
using namespace clang;
@@ -2511,7 +2512,8 @@ void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
static bool compareModuleHeaders(const Module::Header &A,
const Module::Header &B) {
- return A.NameAsWritten < B.NameAsWritten;
+ return std::tie(A.NameAsWritten, A.PathRelativeToRootModuleDirectory) <
+ std::tie(B.NameAsWritten, B.PathRelativeToRootModuleDirectory);
}
/// Parse an umbrella directory declaration.
|
So what breakage is caused by the sorting failure? Can that behavior be tested in some way to validate this change and ensure it doesn't regress in the future? |
@dwblaikie This is not causing a breakage. It is just not working as designed because the sort function has been comparing I found this while investigating issue #73145. The way the code is behaving now, the sort function is acting as a NOP and could be removed. However, I don't think that was the intention of the author of 7ff2914.
Possibly. Let me think on this. |
Yeah, looks like this was undertested when originally committed. Perhaps @benlangmuir can help sort out a test for this? Possibly creating a directory with a few headers & they'll appear in some arbitrary order from the OS in the |
You could try using
However I think the printing would need to be fixed to print the right header name instead of "" first. |
This code was initially written in commit
7ff2914 with the intention of sorting headers according to their path. At the time, the path was saved in field NameAsWritten of Module::Header.
Later, commit e6830b6 added field PathRelativeToRootModuleDirectory to Module::Header and modified ModuleMapParser::parseUmbrellaDirDecl() so that it started to save the header path in the new field and started setting NameAsWritten = "". It didn't modify compareModuleHeaders() in order to adapt it to the new field.
After this commit, the sorting stopped working because it continued comparing only NameAsWritten.
This commit fixes it by treating NameAsWritten and PathRelativeToRootModuleDirectory as a tuple when comparing Module::Header.