[clang-format] incorrect sorting of using declarations if multiple scope operators are present in the declaration
When formatting the following piece of code
using boost::regex;
using boost::regex_constants::icase;
using std::chrono::duration_cast;
using std::chrono::microseconds;
using std::chrono::seconds;
using std::chrono::steady_clock;
using std::move;
using std::string;
clang-format produces
using boost::regex;
using boost::regex_constants::icase;
using std::move;
using std::string;
using std::chrono::duration_cast;
using std::chrono::microseconds;
using std::chrono::seconds;
using std::chrono::steady_clock;
but std::move and std::string should be placed after the std::chrono declarations. This only happens when there are multiple :: in the declaration. Observed with clang-format 13.
@llvm/issue-subscribers-clang-format
I think this is intended behaviour. The documentation of SortUsingDeclarations says:
The order of using declarations is defined as follows: Split the strings by “::” and discard any initial empty strings. The last element of each list is a non-namespace name; all others are namespace names. Sort the lists of names lexicographically, where the sort order of individual names is that all non-namespace names come before all namespace names, and within those groups, names are in case-insensitive lexicographic order.
Emphasis on "all non-namespace names come before all namespace names".
This means that declarations with fewer scope operators always come first. This can also be observed by adding an additional declaration which has an extra scope:
using boost::regex;
using boost::regex_constants::icase;
using std::move;
using std::string;
using std::chrono::duration_cast;
using std::chrono::microseconds;
using std::chrono::seconds;
using std::chrono::steady_clock;
using std::chrono::duration_cast::duration_cast;
Since it has the most scope operators, it comes last when sorted.
I suppose this could be configurable somehow, but that'd be an enhancement request.
I think this is intended behaviour. The documentation of
SortUsingDeclarationssays:The order of using declarations is defined as follows: Split the strings by “::” and discard any initial empty strings. The last element of each list is a non-namespace name; all others are namespace names. Sort the lists of names lexicographically, where the sort order of individual names is that all non-namespace names come before all namespace names, and within those groups, names are in case-insensitive lexicographic order.
Emphasis on "all non-namespace names come before all namespace names". This means that declarations with fewer scope operators always come first. This can also be observed by adding an additional declaration which has an extra scope:
using boost::regex; using boost::regex_constants::icase; using std::move; using std::string; using std::chrono::duration_cast; using std::chrono::microseconds; using std::chrono::seconds; using std::chrono::steady_clock; using std::chrono::duration_cast::duration_cast;Since it has the most scope operators, it comes last when sorted.
I suppose this could be configurable somehow, but that'd be an enhancement request.
This can not expain why using boost::regex_constants::icase; apears before using std::move; I think. The definition of order is a combination of lexicographic order and numerical order of count of scopes.
https://reviews.llvm.org/D141694