misc: Update attribute syntax, and reorganize compiler.hh.

This change replaces the __attribute__ syntax with the now standard [[]]
syntax. It also reorganizes compiler.hh so that all special macros have
some explanatory text saying what they do, and each attribute which has a
standard version can use that if available and what version of c++ it's
standard in is put in a comment.

Also, the requirements as far as where you put [[]] style attributes are
a little more strict than the old school __attribute__ style. The use of
the attribute macros was updated to fit these new, more strict
requirements.

Change-Id: Iace44306a534111f1c38b9856dc9e88cd9b49d2a
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/35219
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Maintainer: Gabe Black <gabeblack@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2020-09-26 18:26:02 -07:00
parent 3c31a214b6
commit b877efa6d4
78 changed files with 242 additions and 208 deletions

View File

@@ -75,15 +75,15 @@ class BmpWriter : public ImgWriter
void write(std::ostream &bmp) const override;
private:
struct FileHeader {
struct M5_ATTR_PACKED FileHeader {
unsigned char magic_number[2];
uint32_t size;
uint16_t reserved1;
uint16_t reserved2;
uint32_t offset;
} M5_ATTR_PACKED;
};
struct InfoHeaderV1 { /* Aka DIB header */
struct M5_ATTR_PACKED InfoHeaderV1 { /* Aka DIB header */
uint32_t Size;
uint32_t Width;
uint32_t Height;
@@ -95,14 +95,14 @@ class BmpWriter : public ImgWriter
uint32_t YPelsPerMeter;
uint32_t ClrUsed;
uint32_t ClrImportant;
} M5_ATTR_PACKED;
};
struct CompleteV1Header {
struct M5_ATTR_PACKED CompleteV1Header {
FileHeader file;
InfoHeaderV1 info;
} M5_ATTR_PACKED;
};
struct BmpPixel32 {
struct M5_ATTR_PACKED BmpPixel32 {
BmpPixel32 &operator=(const Pixel &rhs) {
red = rhs.red;
green = rhs.green;
@@ -115,7 +115,7 @@ class BmpWriter : public ImgWriter
uint8_t green;
uint8_t red;
uint8_t padding;
} M5_ATTR_PACKED;
};
typedef BmpPixel32 PixelType;

View File

@@ -45,43 +45,77 @@
// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
#if defined(__GNUC__) // clang or gcc
# define M5_VAR_USED __attribute__((unused))
# define M5_ATTR_PACKED __attribute__ ((__packed__))
# define M5_NO_INLINE __attribute__ ((__noinline__))
/*
* Attributes that become standard in later versions of c++.
*/
// Use M5_FALLTHROUGH to mark when you're intentionally falling through from
// one case to another in a switch statement.
#if __has_cpp_attribute(fallthrough) // Standard in c++17.
# define M5_FALLTHROUGH [[fallthrough]]
#else
// Not supported, so it's not necessary to avoid warnings.
# define M5_FALLTHROUGH
#endif
// When the return value of a function should not be discarded, mark it with
// M5_NODISCARD.
#if __has_cpp_attribute(nodiscard) // Standard in c++17, with message in c++20.
# define M5_NODISCARD [[nodiscard]]
#else
// Not supported, but it's optional so we can just omit it.
# define M5_NODISCARD
#endif
// When a variable may purposefully not be used, for instance if it's only used
// in debug statements which might be disabled, mark it with M5_VAR_USED.
#if __has_cpp_attribute(maybe_unused) // Standard in c++17.
# define M5_VAR_USED [[maybe_unused]]
#elif defined(__GNUC__)
// gcc and clang support a custom attribute which is essentially the same
// thing.
# define M5_VAR_USED [[gnu::unused]]
#endif
/*
* Compiler specific features.
*/
#if defined(__GNUC__) // clang or gcc.
// Mark a structure as packed, so that no padding is added to its layout. This
// padding might be added to, for instance, ensure certain fields have certain
// alignment.
# define M5_ATTR_PACKED [[gnu::packed]]
// Prevent a function from being inlined.
# define M5_NO_INLINE [[gnu::noinline]]
// Set the visibility of a symbol.
# define M5_PUBLIC [[gnu:visibility("default")]]
# define M5_LOCAL [[gnu::visibility("hidden")]]
// Marker for what should be an unreachable point in the code.
# define M5_UNREACHABLE __builtin_unreachable()
# define M5_PUBLIC __attribute__ ((visibility ("default")))
# define M5_LOCAL __attribute__ ((visibility ("hidden")))
// To mark a branch condition as likely taken, wrap it's condition with
// M5_LIKELY. To mark it as likely not taken, wrap it's condition with
// M5_UNLIKELY. These can be replaced with the standard attributes [[likely]]
// and [[unlikely]] in c++20, although the syntax is different enough that
// we can't do that with direct substitution.
# define M5_LIKELY(cond) __builtin_expect(!!(cond), 1)
# define M5_UNLIKELY(cond) __builtin_expect(!!(cond), 0)
#endif
#if defined(__clang__)
// When a member variable may be unused, mark it with M5_CLASS_VAR_USED. This
// needs to be limitted to clang only since clang warns on these unused
// variables, and g++ will actually warn if you use this attribute since it
// won't do anything there.
#if defined(__clang__) // clang only.
# define M5_CLASS_VAR_USED M5_VAR_USED
#else
# define M5_CLASS_VAR_USED
#endif
// This can be removed once all compilers support C++17
#if defined __has_cpp_attribute
// Note: We must separate this if statement because GCC < 5.0 doesn't
// support the function-like syntax in #if statements.
#if __has_cpp_attribute(fallthrough)
#define M5_FALLTHROUGH [[fallthrough]]
#else
#define M5_FALLTHROUGH
#endif
#if __has_cpp_attribute(nodiscard)
#define M5_NODISCARD [[nodiscard]]
#else
#define M5_NODISCARD
#endif
#else
// Unsupported (and no warning) on GCC < 7.
#define M5_FALLTHROUGH
#define M5_NODISCARD
#endif
#endif // __BASE_COMPILER_HH__

View File

@@ -141,7 +141,7 @@ ElfObject::ElfObject(ImageFileDataPtr ifd) : ObjectFile(ifd)
"No loadable segments in '%s'. ELF file corrupted?\n",
imageData->filename());
for (auto M5_VAR_USED &seg: image.segments())
for (M5_VAR_USED auto &seg: image.segments())
DPRINTFR(Loader, "%s\n", seg);
// We will actually read the sections when we need to load them

View File

@@ -76,7 +76,7 @@ class PngWriter : public ImgWriter
void write(std::ostream &png) const override;
private:
/** Png Pixel type: not containing padding */
struct PngPixel24 {
struct M5_ATTR_PACKED PngPixel24 {
PngPixel24 &operator=(const Pixel &rhs) {
red = rhs.red;
green = rhs.green;
@@ -87,7 +87,7 @@ class PngWriter : public ImgWriter
uint8_t red;
uint8_t green;
uint8_t blue;
} M5_ATTR_PACKED;
};
/**
* Handle to resources used by libpng:

View File

@@ -68,7 +68,7 @@ Group::regStats()
for (auto &g : statGroups) {
if (DTRACE(Stats)) {
const SimObject M5_VAR_USED *so =
M5_VAR_USED const SimObject *so =
dynamic_cast<const SimObject *>(this);
DPRINTF(Stats, "%s: regStats in group %s\n",
so ? so->name() : "?",

View File

@@ -96,7 +96,7 @@ class VncInput : public SimObject
ClientCutText = 6
};
struct PixelFormat {
struct M5_ATTR_PACKED PixelFormat {
uint8_t bpp;
uint8_t depth;
uint8_t bigendian;
@@ -108,48 +108,48 @@ class VncInput : public SimObject
uint8_t greenshift;
uint8_t blueshift;
uint8_t padding[3];
} M5_ATTR_PACKED;
};
struct PixelFormatMessage {
struct M5_ATTR_PACKED PixelFormatMessage {
uint8_t type;
uint8_t padding[3];
PixelFormat px;
} M5_ATTR_PACKED;
};
struct PixelEncodingsMessage {
struct M5_ATTR_PACKED PixelEncodingsMessage {
uint8_t type;
uint8_t padding;
uint16_t num_encodings;
} M5_ATTR_PACKED;
};
struct FrameBufferUpdateReq {
struct M5_ATTR_PACKED FrameBufferUpdateReq {
uint8_t type;
uint8_t incremental;
uint16_t x;
uint16_t y;
uint16_t width;
uint16_t height;
} M5_ATTR_PACKED;
};
struct KeyEventMessage {
struct M5_ATTR_PACKED KeyEventMessage {
uint8_t type;
uint8_t down_flag;
uint8_t padding[2];
uint32_t key;
} M5_ATTR_PACKED;
};
struct PointerEventMessage {
struct M5_ATTR_PACKED PointerEventMessage {
uint8_t type;
uint8_t button_mask;
uint16_t x;
uint16_t y;
} M5_ATTR_PACKED;
};
struct ClientCutTextMessage {
struct M5_ATTR_PACKED ClientCutTextMessage {
uint8_t type;
uint8_t padding[3];
uint32_t length;
} M5_ATTR_PACKED;
};
typedef VncInputParams Params;
VncInput(const Params *p);

View File

@@ -378,7 +378,7 @@ VncServer::checkProtocolVersion()
{
assert(curState == WaitForProtocolVersion);
size_t len M5_VAR_USED;
M5_VAR_USED size_t len;
char version_string[13];
// Null terminate the message so it's easier to work with

View File

@@ -106,33 +106,33 @@ class VncServer : public VncInput
NormalPhase
};
struct ServerInitMsg {
struct M5_ATTR_PACKED ServerInitMsg {
uint16_t fbWidth;
uint16_t fbHeight;
PixelFormat px;
uint32_t namelen;
char name[2]; // just to put M5 in here
} M5_ATTR_PACKED;
};
struct FrameBufferUpdate {
struct M5_ATTR_PACKED FrameBufferUpdate {
uint8_t type;
uint8_t padding;
uint16_t num_rects;
} M5_ATTR_PACKED;
};
struct FrameBufferRect {
struct M5_ATTR_PACKED FrameBufferRect {
uint16_t x;
uint16_t y;
uint16_t width;
uint16_t height;
int32_t encoding;
} M5_ATTR_PACKED;
};
struct ServerCutText {
struct M5_ATTR_PACKED ServerCutText {
uint8_t type;
uint8_t padding[3];
uint32_t length;
} M5_ATTR_PACKED;
};
/** @} */