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

Clang-format using templates and greater than comparison

Open Roelio81 opened this issue 3 years ago • 3 comments

I noticed some weird result when running clang-format on our code base. The issue seems to be linked to the use of templates and a greater than comparison. The following snippet is the code that needs to be formatted:

#include <iostream>
#include <tuple>

template <typename T>
auto test(T t) noexcept -> void {
    if constexpr (std::tuple_size_v<T> > 0)
        std::cout << "Not empty" << std::endl;
    else
        std::cout << "Empty" << std::endl;
}

int main(void) {
    test(std::tuple{});
    test(std::tuple{1, "g"});
}

Link to compiler explorer

clang-format 13 (and also more recent versions it seems), changes the following line like this:

-if constexpr (std::tuple_size_v<T> > 0)
+if constexpr (std::tuple_size_v < T >> 0)

The formatting has been set to Google, all other configuration options are just using default values. Note: since tuple_size_v returns an unsigned integer, we changed our code to if constexpr (std::tuple_size_v<T> != 0) to work around this issue.

Roelio81 avatar Jan 02 '23 09:01 Roelio81

It seems like the angles is parsed as not matched due to this line: TokenAnnotator.cpp:169.

Backl1ght avatar Jan 02 '23 14:01 Backl1ght

@llvm/issue-subscribers-clang-format

llvmbot avatar Jan 02 '23 15:01 llvmbot

https://reviews.llvm.org/D140843

Backl1ght avatar Jan 02 '23 15:01 Backl1ght