From 6c61004b06c751258a5783507d10b51acf7ead03 Mon Sep 17 00:00:00 2001 From: nhmall Date: Sat, 11 Jun 2022 13:52:58 -0400 Subject: [PATCH] g++-12 bits, mostly Qt5 related I forced a test compile to -std=c++20 mostly to see what we would be up against. There was only a small number of things and they are corrected in this commit. c++20 has some issues with comparisons and bit twiddling between different enums. The vendor-supplied Qt5 header files triggered some of those issues as well, so the qt_pre.h and qt_post.h NetHack header files were adjusted to make those new warnings go away. I have not tested Qt6 under the new compiler and c++ version yet. Because there are multiple pragmas in qt_pre.h now, the conditional ifdef structure in there was modified a little to make maintenance simpler and have a single pragma push at the top. The pragma pop comes after the Qt vendor-supplied header files, and is done in qt_post.h. The display.h macro cmap_to_glyph() was used in a Qt c++ file and triggered a series of warnings because of that. Rather than write c++20-friendly versions of those macros, the simple fix is to provide a function on the C side of things to front the cmap_to_glyph() macro, so fn_cmap_to_glyph() was added. Also thrown into this commit, PatR picked up on the fact that for yesterday's new warning in qt_menu.cpp, the compiler had correctly picked up on the fact that the format range of the variable 'cash' had been correctly upper-capped at 999999999L in the warning message because of an assignment prior. He suggested that perhaps by also adding if (cash < 0) cash = 0; the warning might be eliminated altogether. After a test, that was proven to be correct, so yesterday's more-kludgy change is reverted and replaced with that variable variable restriction ahead of the snprintf(). --- include/extern.h | 1 + src/display.c | 12 ++++++++++++ win/Qt/qt_bind.cpp | 5 +++++ win/Qt/qt_inv.cpp | 2 +- win/Qt/qt_menu.cpp | 13 ++++++++++--- win/Qt/qt_post.h | 4 +++- win/Qt/qt_pre.h | 37 +++++++++++++++++++++++++++++++++---- win/Qt/qt_rip.cpp | 10 ++++++++-- win/Qt/qt_stat.cpp | 16 ++++++++++++++++ win/Qt/qt_yndlg.cpp | 5 +++++ 10 files changed, 94 insertions(+), 11 deletions(-) diff --git a/include/extern.h b/include/extern.h index 9e5e46726..160f0829a 100644 --- a/include/extern.h +++ b/include/extern.h @@ -443,6 +443,7 @@ extern void unset_seenv(struct rm *, int, int, int, int); extern int warning_of(struct monst *); extern void map_glyphinfo(xchar, xchar, int, unsigned, glyph_info *); extern void reset_glyphmap(enum glyphmap_change_triggers trigger); +extern int fn_cmap_to_glyph(int); /* ### do.c ### */ diff --git a/src/display.c b/src/display.c index 4a6e20aa3..da0dd7602 100644 --- a/src/display.c +++ b/src/display.c @@ -3486,4 +3486,16 @@ wall_angle(struct rm *lev) return idx; } +/* + * c++ 20 has problems with some of the display.h macros because + * comparisons and bit-fiddling and math between different enums + * is deprecated. + * Create function versions of some of the macros used in some + * NetHack c++ source files (Qt) for use there. + */ +int +fn_cmap_to_glyph(int cmap) +{ + return cmap_to_glyph(cmap); +} /*display.c*/ diff --git a/win/Qt/qt_bind.cpp b/win/Qt/qt_bind.cpp index c27a6b643..b5ee273cb 100644 --- a/win/Qt/qt_bind.cpp +++ b/win/Qt/qt_bind.cpp @@ -139,7 +139,12 @@ NetHackQtBind::qt_Splash() if (qt_compact_mode) { splash->showMaximized(); } else { +#if __cplusplus >= 202002L + splash->setFrameStyle(static_cast(QFrame::WinPanel) + | static_cast(QFrame::Raised)); +#else splash->setFrameStyle(QFrame::WinPanel | QFrame::Raised); +#endif splash->setLineWidth(10); splash->adjustSize(); splash->show(); diff --git a/win/Qt/qt_inv.cpp b/win/Qt/qt_inv.cpp index 53a32ac70..8fec2dfb9 100644 --- a/win/Qt/qt_inv.cpp +++ b/win/Qt/qt_inv.cpp @@ -137,7 +137,7 @@ void NetHackQtInvUsageWindow::drawWorn(QPainter &painter, obj *nhobj, nhUse(alttip); #endif // an empty slot is shown as floor tile unless it's always empty - glyph = canbe ? cmap_to_glyph(S_room) : GLYPH_UNEXPLORED; + glyph = canbe ? fn_cmap_to_glyph(S_room) : GLYPH_UNEXPLORED; } map_glyphinfo(0, 0, glyph, 0, &gi); /* this skirts the defined * interface unfortunately */ diff --git a/win/Qt/qt_menu.cpp b/win/Qt/qt_menu.cpp index bc6810b0f..d7619c461 100644 --- a/win/Qt/qt_menu.cpp +++ b/win/Qt/qt_menu.cpp @@ -170,7 +170,12 @@ NetHackQtMenuWindow::NetHackQtMenuWindow(QWidget *parent) : QGridLayout *grid = new QGridLayout(); table->setColumnCount(5); +#if __cplusplus >= 202002L + table->setFrameStyle(static_cast(QFrame::Panel) + | static_cast(QFrame::Raised)); +#else table->setFrameStyle(QFrame::Panel|QFrame::Sunken); +#endif table->setLineWidth(2); // note: this is not row spacing table->setShowGrid(false); table->horizontalHeader()->hide(); @@ -1058,7 +1063,7 @@ void NetHackQtTextWindow::UseRIP(int how, time_t when) char buf[BUFSZ]; char *dpx; - int line, snpres; + int line; /* Put name on stone */ (void) snprintf(rip_line[NAME_LINE], STONE_LINE_LEN + 1, @@ -1077,10 +1082,12 @@ void NetHackQtTextWindow::UseRIP(int how, time_t when) long cash = std::max(g.done_money, 0L); /* force less that 10 digits to satisfy elaborate format checking; it's arbitrary but still way, way more than could ever be needed */ + if (cash < 0) + cash = 0; if (cash > 999999999L) cash = 999999999L; - snpres = snprintf(rip_line[GOLD_LINE], STONE_LINE_LEN + 1, "%ld Au", cash); - nhUse(snpres); + (void) snprintf(rip_line[GOLD_LINE], STONE_LINE_LEN + 1, "%ld Au", cash); + /* Put together death description */ formatkiller(buf, sizeof buf, how, FALSE); //str_copy(buf, killer, SIZE(buf)); diff --git a/win/Qt/qt_post.h b/win/Qt/qt_post.h index 7cfcdb42a..7c9724473 100644 --- a/win/Qt/qt_post.h +++ b/win/Qt/qt_post.h @@ -6,10 +6,12 @@ * #include after . */ +#if defined(__cplusplus) #ifdef __clang__ #pragma clang diagnostic pop -#elif defined(__GNUC__) && defined(__cplusplus) +#elif defined(__GNUC__) #pragma GCC diagnostic pop #endif /* compiler-specific bits */ +#endif /* __cplusplus */ /*qt_post.h*/ diff --git a/win/Qt/qt_pre.h b/win/Qt/qt_pre.h index 980196aeb..db3830016 100644 --- a/win/Qt/qt_pre.h +++ b/win/Qt/qt_pre.h @@ -17,14 +17,22 @@ #undef min #undef max +#if defined(__cplusplus) + +#ifdef __clang__ +#pragma clang diagnostic push +#endif +#ifdef __GNUC__ +#pragma GCC diagnostic push +#endif +/* the diagnostic pop is in qt_post.h */ + +#ifdef __clang__ /* disable warnings for shadowed names; some of the Qt prototypes use placeholder argument names which conflict with nethack variables ('g', 'u', a couple of others) */ -#ifdef __clang__ -#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wshadow" -#elif defined(__GNUC__) && defined(__cplusplus) -#pragma GCC diagnostic push +#elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wshadow" #endif @@ -37,5 +45,26 @@ #define QFM_WIDTH(foo) horizontalAdvance(foo) #endif +#if __cplusplus >= 202002L +/* c++20 or newer */ +#if QT_VERSION < 0x060000 +/* + * qt5/QtWidgets/qsizepolicy.h + * Qt5 header file issue under c++ 20 + * + * warning: bitwise operation between different enumeration types + * ‘QSizePolicy::Policy’ and ‘QSizePolicy::PolicyFlag’ + * is deprecated [-Wdeprecated-enum-enum-conversion] + */ +#if defined(__GNUC__) +#pragma GCC diagnostic ignored "-Wdeprecated-enum-enum-conversion" +#endif +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion" +#endif +#endif /* QT_VERSION < 0x060000 */ +#endif /* __cplusplus >= 202002L */ +#endif /* __cplusplus */ + /*qt_pre.h*/ diff --git a/win/Qt/qt_rip.cpp b/win/Qt/qt_rip.cpp index b4aeb1a3a..bcf105688 100644 --- a/win/Qt/qt_rip.cpp +++ b/win/Qt/qt_rip.cpp @@ -79,8 +79,14 @@ void NetHackQtRIP::paintEvent(QPaintEvent* event UNUSED) painter.begin(this); painter.drawPixmap(pix_x,pix_y,*pixmap); for (int i=0; i= 202002L + static_cast(Qt::TextDontClip) + | static_cast(Qt::AlignHCenter), +#else + Qt::TextDontClip|Qt::AlignHCenter, +#endif + line[i]); } painter.end(); } diff --git a/win/Qt/qt_stat.cpp b/win/Qt/qt_stat.cpp index 6e33f7752..ad56429c1 100644 --- a/win/Qt/qt_stat.cpp +++ b/win/Qt/qt_stat.cpp @@ -222,15 +222,31 @@ NetHackQtStatusWindow::NetHackQtStatusWindow() : ride.setIcon(p_ride, "riding"); // separator lines +#if __cplusplus >= 202002L + hline1.setFrameStyle(static_cast(QFrame::HLine) + | static_cast(QFrame::Sunken)); + hline2.setFrameStyle(static_cast(QFrame::HLine) + | static_cast(QFrame::Sunken)); + hline3.setFrameStyle(static_cast(QFrame::HLine) + | static_cast(QFrame::Sunken)); +#else hline1.setFrameStyle(QFrame::HLine | QFrame::Sunken); hline2.setFrameStyle(QFrame::HLine | QFrame::Sunken); hline3.setFrameStyle(QFrame::HLine | QFrame::Sunken); +#endif hline1.setLineWidth(1); hline2.setLineWidth(1); hline3.setLineWidth(1); // vertical separators for condensed layout (statuslines:2) +#if __cplusplus >= 202002L + vline1.setFrameStyle(static_cast(QFrame::VLine) + | static_cast(QFrame::Sunken)); + vline2.setFrameStyle(static_cast(QFrame::VLine) + | static_cast(QFrame::Sunken)); +#else vline1.setFrameStyle(QFrame::VLine | QFrame::Sunken); vline2.setFrameStyle(QFrame::VLine | QFrame::Sunken); +#endif vline1.setLineWidth(1); // separates Alignment from Charisma vline2.setLineWidth(1); vline2.hide(); // padding to keep row 2 aligned with row 1, never shown diff --git a/win/Qt/qt_yndlg.cpp b/win/Qt/qt_yndlg.cpp index cd1e7e61d..b558b3c53 100644 --- a/win/Qt/qt_yndlg.cpp +++ b/win/Qt/qt_yndlg.cpp @@ -352,7 +352,12 @@ char NetHackQtYnDialog::Exec() } else { QLabel label(qlabel,this); QPushButton cancel("Dismiss",this); +#if __cplusplus >= 202002L + label.setFrameStyle(static_cast(QFrame::Box) + | static_cast(QFrame::Sunken)); +#else label.setFrameStyle(QFrame::Box|QFrame::Sunken); +#endif label.setAlignment(Qt::AlignCenter); label.resize(fontMetrics().QFM_WIDTH(qlabel)+60,30+fontMetrics().height()); cancel.move(width()/2-cancel.width()/2,label.geometry().bottom()+8); -- 2.50.1