Skip to content
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

C++20 [[likely]] / [[unlikely]] attribute support #212

Open
XuehaiPan opened this issue Oct 19, 2022 · 1 comment · May be fixed by #265
Open

C++20 [[likely]] / [[unlikely]] attribute support #212

XuehaiPan opened this issue Oct 19, 2022 · 1 comment · May be fixed by #265
Assignees

Comments

@XuehaiPan
Copy link

XuehaiPan commented Oct 19, 2022

In C++ attribute: likely, unlikely (since C++20), we can add [[likely]] / [[unlikely]] on if-else clauses. But cpplint does not recognize the [[likely]] / [[unlikely]] attributes after if / else / else if:

Source file:

// foo.cpp
// Copyright 2022 <test>

#include <iostream>

int fib1(int n) {
    if (n > 1) [[likely]] {
        int a;
        return fib1(n - 1) + fib1(n - 2);
    } else if (n == 2) [[unlikely]] {
        int b;
        return 1;
    } else [[unlikely]] {
        if (n == 1) [[likely]]
            return 1;
        else [[unlikely]]
            return 0;
    }
}

int fib2(int n) {
    switch (n) {
        [[unlikely]] case 0: return 0;
        case 1:
            [[fallthrough]];
        case 2:
            [[fallthrough]];
        [[unlikely]] case 3: {
            return 1;
        }
        [[likely]] default:
            return fib2(n - 1) + fib2(n - 2);
    }
}

Command-line:

$ pip3 install -I git+https://github.com/cpplint/cpplint.git
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting git+https://github.com/cpplint/cpplint.git
  Cloning https://github.com/cpplint/cpplint.git to /tmp/pip-req-build-6nkph3fd
  Running command git clone --filter=blob:none --quiet https://github.com/cpplint/cpplint.git /tmp/pip-req-build-6nkph3fd
  Resolved https://github.com/cpplint/cpplint.git to commit fa12a0bbdafa15291276ddd2a2dcd2ac7a2ce4cb
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: cpplint
  Building wheel for cpplint (setup.py) ... done
  Created wheel for cpplint: filename=cpplint-1.6.1-py3-none-any.whl size=77266 sha256=3e95bbfd3de2b8d61d561339213e6a4ae741e8bb110b34c9b6f626c55cd325c9
  Stored in directory: /tmp/pip-ephem-wheel-cache-d42ge4uo/wheels/af/90/88/663d23197700a259f2a9199aef67ee9977e21f6d1980257857
Successfully built cpplint
Installing collected packages: cpplint
Successfully installed cpplint-1.6.1
$ cpplint foo.cpp
foo.cpp:7:  If/else bodies with multiple statements require braces  [readability/braces] [4]
foo.cpp:10:  If/else bodies with multiple statements require braces  [readability/braces] [4]
foo.cpp:13:  Else clause should never be on same line as else (use 2 lines)  [whitespace/newline] [4]
foo.cpp:13:  If/else bodies with multiple statements require braces  [readability/braces] [4]
foo.cpp:16:  Else clause should never be on same line as else (use 2 lines)  [whitespace/newline] [4]
Done processing foo.cpp
Total errors found: 5
@XuehaiPan XuehaiPan changed the title C++20 [[likely]] / [[unlikely]] attribute support for else clause C++20 [[likely]] / [[unlikely]] attribute support Oct 19, 2022
@tkruse
Copy link

tkruse commented Jan 29, 2023

Than ks for the report, cpplint currently does not support any braking c++20 syntax elements, and I don't know if it ever will (due to lack of maintainers, and googles lack of public cpplint updates).

@aaronliu0130 aaronliu0130 self-assigned this Feb 29, 2024
aaronliu0130 added a commit to aaronliu0130/cpplint that referenced this issue Mar 17, 2024
1. Fixes cpplint#212
2. else statements that have braces on both sides but have the second-to-last } on a separate line will no longer claim we need braces on both sides
3. Fix single-line if statements being yelled at to split their lines, something contradictory to the style guide
4. Fixes relevant tests, including adding the new "TestLintContains" and "TestLintNotContains" methods. (ik the latter may have a grammar error but who cares, this is consistent with the former's name)
5. Add a todo for "This exception does not apply to multi-keyword statements like if ... else or do ... while."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants