File: constant_expr.i

package info (click to toggle)
swig 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 46,232 kB
  • sloc: cpp: 54,631; ansic: 29,122; java: 17,530; python: 12,505; cs: 10,369; ruby: 7,232; yacc: 6,477; makefile: 5,965; javascript: 5,520; sh: 5,415; perl: 4,187; php: 3,693; ml: 2,187; lisp: 2,056; tcl: 1,991; xml: 115
file content (69 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
%module constant_expr;
/* Tests of constant expressions (C++ version). */

%include "constant_expr_c.i"

%inline %{

// Testcase from https://sourceforge.net/p/swig/bugs/1139/
template<typename Tp>
struct SizeInfo {
enum {
isLarge = (sizeof(Tp)>sizeof(void*)),
isPointer = false
};
};

/* Regression test for #300, fixed in 4.1.0.
 *
 * Now `9%a` without a space after the `%` is handled as a modulus operator,
 * but it gave a cryptic `Syntax error in input(1)` before SWIG 3.0.4, and from
 * SWIG 3.0.4 until 4.1.0, `Unknown directive '%a'`.
 */
int a;
int test2(int b = 9%a) { return b; }

/* Example from manual, adapted to avoid C++11 requirement. */
namespace fakestd {
  template<typename T, unsigned N>
  class array {
      T a[N];
    public:
      array() {}
  };
}
void bar(fakestd::array<int, (1<2? 100 : 50)> *x) { }

%}

// Regression test for #2919, fixed in 4.3.0.
//
// sizeof() didn't work on complex expressions or types.
//
// This is just a parsing test for SWIG as it uses C++11 features.
#include <type_traits>
template <typename T>
class InternalHelper {
public:
// Original source: https://github.com/protocolbuffers/protobuf/blob/v20.2/src/google/protobuf/arena.h#L449-L458
    template <typename U>
    static char DestructorSkippable(const typename U::DestructorSkippable_*);
    template <typename U>
    static double DestructorSkippable(...);

    typedef std::integral_constant<
        bool, sizeof(DestructorSkippable<T>(static_cast<const T*>(0))) ==
                      sizeof(char) ||
                  std::is_trivially_destructible<T>::value>
        is_destructor_skippable;

    // This is nonsensical, but provides a regression test for this not working with alignof() either.
    typedef std::integral_constant<
        bool, alignof(DestructorSkippable<T>(static_cast<const T*>(0))) ==
                      alignof(char) ||
                  std::is_trivially_destructible<T>::value>
        test_alignof_too;
};

%constant int WSTRING_LIT_LEN1 = (sizeof(L"1234")/sizeof(wchar_t) - 1);
%constant int WSTRING_LIT_LEN2 = (sizeof(L"12" L"34")/sizeof(wchar_t) - 1);