• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_FML_COMPILER_SPECIFIC_H_
6 #define FLUTTER_FML_COMPILER_SPECIFIC_H_
7 
8 #if !defined(__GNUC__) && !defined(__clang__) && !defined(_MSC_VER)
9 #error Unsupported compiler.
10 #endif
11 
12 // Annotate a variable indicating it's ok if the variable is not used.
13 // (Typically used to silence a compiler warning when the assignment
14 // is important for some other reason.)
15 // Use like:
16 //   int x = ...;
17 //   FML_ALLOW_UNUSED_LOCAL(x);
18 #define FML_ALLOW_UNUSED_LOCAL(x) false ? (void)x : (void)0
19 
20 // Annotate a typedef or function indicating it's ok if it's not used.
21 // Use like:
22 //   typedef Foo Bar ALLOW_UNUSED_TYPE;
23 #if defined(__GNUC__) || defined(__clang__)
24 #define FML_ALLOW_UNUSED_TYPE __attribute__((unused))
25 #else
26 #define FML_ALLOW_UNUSED_TYPE
27 #endif
28 
29 // Annotate a function indicating it should not be inlined.
30 // Use like:
31 //   NOINLINE void DoStuff() { ... }
32 #if defined(__GNUC__) || defined(__clang__)
33 #define FML_NOINLINE __attribute__((noinline))
34 #elif defined(_MSC_VER)
35 #define FML_NOINLINE __declspec(noinline)
36 #endif
37 
38 // Specify memory alignment for structs, classes, etc.
39 // Use like:
40 //   class FML_ALIGNAS(16) MyClass { ... }
41 //   FML_ALIGNAS(16) int array[4];
42 #if defined(__GNUC__) || defined(__clang__)
43 #define FML_ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
44 #elif defined(_MSC_VER)
45 #define FML_ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
46 #endif
47 
48 // Return the byte alignment of the given type (available at compile time).
49 // Use like:
50 //   FML_ALIGNOF(int32)  // this would be 4
51 #if defined(__GNUC__) || defined(__clang__)
52 #define FML_ALIGNOF(type) __alignof__(type)
53 #elif defined(_MSC_VER)
54 #define FML_ALIGNOF(type) __alignof(type)
55 #endif
56 
57 // Annotate a function indicating the caller must examine the return value.
58 // Use like:
59 //   int foo() FML_WARN_UNUSED_RESULT;
60 // To explicitly ignore a result, see |ignore_result()| in base/macros.h.
61 #if defined(__GNUC__) || defined(__clang__)
62 #define FML_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
63 #else
64 #define FML_WARN_UNUSED_RESULT
65 #endif
66 
67 // Tell the compiler a function is using a printf-style format string.
68 // |format_param| is the one-based index of the format string parameter;
69 // |dots_param| is the one-based index of the "..." parameter.
70 // For v*printf functions (which take a va_list), pass 0 for dots_param.
71 // (This is undocumented but matches what the system C headers do.)
72 #if defined(__GNUC__) || defined(__clang__)
73 #define FML_PRINTF_FORMAT(format_param, dots_param) \
74   __attribute__((format(printf, format_param, dots_param)))
75 #else
76 #define FML_PRINTF_FORMAT(format_param, dots_param)
77 #endif
78 
79 #endif  // FLUTTER_FML_COMPILER_SPECIFIC_H_
80