Latest Posts

Topic: Struggling with fix_formatting

tothxa
Avatar
Joined: 2021-03-24, 12:44
Posts: 99
Ranking
Likes to be here
Posted at: 2021-09-10, 09:49

Latest chapter:

How can I get it to accept a commit that includes formatting fixes? This one was right next to the part I was modifying. Running clang-format on the file would change it to the committed version. But the "formatting your code" github action fails with this diff, that wants to revert to the old, broken indentation:

@@ -249,7 +249,7 @@ void ConstructionSiteWindow::init(bool avoid_fastclick, bool workarea_preview_wa
                        game_->send_player_militarysite_set_soldier_preference(
                           *construction_site_.get(ibase()->egbase()),
                           state ? Widelands::SoldierPreference::kRookies :
-                                  Widelands::SoldierPreference::kHeroes);
+                             Widelands::SoldierPreference::kHeroes);
                    } else {
                        NEVER_HERE();  // TODO(Nordfriese / Scenario Editor): implement
                    }

Previous one:

I think this is a bug in clang-format. There's this code in mapview.cc in MapView::croll_map():

        // numpad keys
        const bool kNumlockOff = !(SDL_GetModState() & KMOD_NUM);
#define kNP(x) const bool kNP##x = kNumlockOff && get_key_state(SDL_SCANCODE_KP_##x);
        kNP(1) kNP(2) kNP(3) kNP(4) kNP(6) kNP(7) kNP(8) kNP(9)
#undef kNP

           // set the scrolling distance
           const uint16_t xres = g_gr->get_xres();
        const uint16_t yres = g_gr->get_yres();

        uint16_t scroll_distance_x = xres / 8;
        uint16_t scroll_distance_y = yres / 8;

I tried several ways to work it around, but I couldn't. It insists on treating the comment as if it belonged to the undef. I guess a #define / #undef pair in the middle of a function definition is highly unusual, but I havent't the faintest idea why the comment pulls the next line of code with it.

Is there a way to tell fix_formatting.py that clang-format is wrong?


Top Quote
Nordfriese
Avatar
Joined: 2017-01-17, 18:07
Posts: 1454
Ranking
One Elder of Players
Location: 0x55555d3a34c0
Posted at: 2021-09-10, 10:18

Regarding the first one issue: The formatting results are highly specific to the clang-format version you use. An up-/downgrade to a different clang-format version reformats close to 100 files in the repo. That's why it's run only as an action, not locally on the developer's machine (we used to do that – it was horrible…).

The action only actually commits any changes if the branch has an open pull request to master. Otherwise no commit is triggered to avoid annoying the developer. You can work around this by temporarily editing .github/workflows/format.yaml:88,

Re the second issue: clang-format is prone to this sort of errors. When it's really wrong (like here) you can surround the lines with

// clang-format off
…some code here…
// clang-format on

to disable all formatting.


Top Quote
tothxa
Avatar
Joined: 2021-03-24, 12:44
Posts: 99
Ranking
Likes to be here
Posted at: 2021-09-10, 16:06

Thank you for the explanation.


Top Quote