cpp

Coverage Report

Created: 2022-09-21 22:22

/home/andy/git/oilshell/oil/mycpp/common.h
Line
Count
Source (jump to first uncovered line)
1
// common.h
2
//
3
// A grab bag of definitions needed in multiple places.
4
5
#ifndef COMMON_H
6
#define COMMON_H
7
8
#include <assert.h>  // assert()
9
#include <ctype.h>   // isalpha(), isdigit()
10
#include <limits.h>  // CHAR_BIT
11
#include <stdarg.h>  // va_list, etc.
12
#include <stddef.h>  // max_align_t
13
#include <stdint.h>  // uint8_t
14
#include <stdio.h>   // vprintf
15
#include <stdlib.h>
16
#include <string.h>  // strlen
17
18
#include <initializer_list>
19
20
// TODO(Jesse): Put NotImplemented on a compile-time switch such that we cannot
21
// make a release build if we're not finished implementing the interpreter.
22
// ie.
23
//
24
// #if OIL_INTERNAL
25
//   #define NotImplemented() assert(!"Not Implemented")
26
// #else
27
//   #define NotImplemented() NOT IMPLENTED !!! // Intentionally a compile error
28
// #endif
29
//
30
//
31
0
#define NotImplemented() assert(!"Not Implemented")
32
0
#define InvalidCodePath() assert(!"Invalid Code Path")
33
34
#define COMMA ,
35
36
// Prevent silent copies
37
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
38
  TypeName(TypeName&) = delete;            \
39
  void operator=(TypeName) = delete;
40
41
// log() is for hand-written code, not generated
42
43
265
inline void log(const char* fmt, ...) {
44
265
  va_list args;
45
265
  va_start(args, fmt);
46
265
  vprintf(fmt, args);
47
265
  va_end(args);
48
265
  puts("");
49
265
}
50
51
// I'm not sure why this matters but we get crashes when aligning to 8 bytes.
52
// That is annoying.
53
// Example: we get a crash in cpp/frontend_flag_spec.cc
54
// auto out = new flag_spec::_FlagSpecAndMore();
55
//
56
// https://stackoverflow.com/questions/52531695/int128-alignment-segment-fault-with-gcc-o-sse-optimize
57
constexpr int kMask = alignof(max_align_t) - 1;  // e.g. 15 or 7
58
59
// Align returned pointers to the worst case of 8 bytes (64-bit pointers)
60
1
inline size_t aligned(size_t n) {
61
  // https://stackoverflow.com/questions/2022179/c-quick-calculation-of-next-multiple-of-4
62
  // return (n + 7) & ~7;
63
1
  return (n + kMask) & ~kMask;
64
1
}
65
66
const int kMaxRoots = KiB(4);
67
68
#endif  // COMMON_H