llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-format] incorrect sorting of using declarations if multiple scope operators are present in the declaration

Open polaris6933 opened this issue 3 years ago • 4 comments

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.

polaris6933 avatar Jan 11 '23 12:01 polaris6933

@llvm/issue-subscribers-clang-format

llvmbot avatar Jan 11 '23 15:01 llvmbot

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.

rymiel avatar Jan 11 '23 17:01 rymiel

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.

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.

Backl1ght avatar Jan 11 '23 17:01 Backl1ght

https://reviews.llvm.org/D141694

Backl1ght avatar Jan 13 '23 16:01 Backl1ght