Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
tokenize.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#ifdef __cplusplus
17
18#include <cstddef>
19#include <cstdint>
20#include <string_view>
21
22#else
23
24#include <stddef.h>
25#include <stdint.h>
26
27#endif // __cplusplus
28
29#include "pw_polyfill/static_assert.h"
30#include "pw_preprocessor/arguments.h"
31#include "pw_preprocessor/compiler.h"
32#include "pw_preprocessor/concat.h"
33#include "pw_preprocessor/util.h"
34#include "pw_tokenizer/internal/argument_types.h"
35#include "pw_tokenizer/internal/tokenize_string.h"
36
39typedef uint32_t pw_tokenizer_Token;
40
41// Strings may optionally be tokenized to a domain. Strings in different
42// domains can be processed separately by the token database tools. Each domain
43// in use must have a corresponding section declared in the linker script. See
44// `pw_tokenizer_linker_sections.ld` for more details.
45//
46// The default domain is an empty string.
47#define PW_TOKENIZER_DEFAULT_DOMAIN ""
48
65#define PW_TOKENIZE_STRING(...) \
66 PW_DELEGATE_BY_ARG_COUNT(_PW_TOKENIZE_STRING_, __VA_ARGS__)
67
68#define _PW_TOKENIZE_STRING_1(string_literal) \
69 PW_TOKENIZE_STRING_DOMAIN(PW_TOKENIZER_DEFAULT_DOMAIN, string_literal)
70
71#define _PW_TOKENIZE_STRING_2(domain, string_literal) \
72 PW_TOKENIZE_STRING_DOMAIN(domain, string_literal)
73
86#define PW_TOKENIZE_STRING_EXPR(...) \
87 PW_DELEGATE_BY_ARG_COUNT(_PW_TOKENIZE_STRING_EXPR_, __VA_ARGS__)
88
89#define _PW_TOKENIZE_STRING_EXPR_1(string_literal) \
90 _PW_TOKENIZE_STRING_EXPR_2(PW_TOKENIZER_DEFAULT_DOMAIN, string_literal)
91
92#define _PW_TOKENIZE_STRING_EXPR_2(domain, string_literal) \
93 [&] { \
94 constexpr uint32_t lambda_ret_token = \
95 PW_TOKENIZE_STRING_DOMAIN(domain, string_literal); \
96 return lambda_ret_token; \
97 }()
98
101#define PW_TOKENIZE_STRING_DOMAIN(domain, string_literal) \
102 PW_TOKENIZE_STRING_MASK(domain, UINT32_MAX, string_literal)
103
106#define PW_TOKENIZE_STRING_DOMAIN_EXPR(domain, string_literal) \
107 [&] { \
108 constexpr uint32_t lambda_ret_token = \
109 PW_TOKENIZE_STRING_DOMAIN(domain, string_literal); \
110 return lambda_ret_token; \
111 }()
112
116#define PW_TOKENIZE_STRING_MASK(domain, mask, string_literal) \
117 /* assign to a variable */ _PW_TOKENIZER_MASK_TOKEN(mask, string_literal); \
118 \
119 static_assert(0 < (mask) && (mask) <= UINT32_MAX, \
120 "Tokenizer masks must be non-zero uint32_t values."); \
121 \
122 PW_TOKENIZER_DEFINE_TOKEN( \
123 _PW_TOKENIZER_MASK_TOKEN(mask, string_literal), domain, string_literal)
124
128#define PW_TOKENIZE_STRING_MASK_EXPR(domain, mask, string_literal) \
129 [&] { \
130 constexpr uint32_t lambda_ret_token = \
131 PW_TOKENIZE_STRING_MASK(domain, mask, string_literal); \
132 return lambda_ret_token; \
133 }()
134
135#define _PW_TOKENIZER_MASK_TOKEN(mask, string_literal) \
136 ((pw_tokenizer_Token)(mask) & PW_TOKENIZER_STRING_TOKEN(string_literal))
137
168#define PW_TOKENIZE_TO_BUFFER(buffer, buffer_size_pointer, format, ...) \
169 PW_TOKENIZE_TO_BUFFER_DOMAIN(PW_TOKENIZER_DEFAULT_DOMAIN, \
170 buffer, \
171 buffer_size_pointer, \
172 format, \
173 __VA_ARGS__)
174
177#define PW_TOKENIZE_TO_BUFFER_DOMAIN( \
178 domain, buffer, buffer_size_pointer, format, ...) \
179 PW_TOKENIZE_TO_BUFFER_MASK( \
180 domain, UINT32_MAX, buffer, buffer_size_pointer, format, __VA_ARGS__)
181
184#define PW_TOKENIZE_TO_BUFFER_MASK( \
185 domain, mask, buffer, buffer_size_pointer, format, ...) \
186 do { \
187 PW_TOKENIZE_FORMAT_STRING(domain, mask, format, __VA_ARGS__); \
188 _pw_tokenizer_ToBuffer(buffer, \
189 buffer_size_pointer, \
190 PW_TOKENIZER_REPLACE_FORMAT_STRING(__VA_ARGS__)); \
191 } while (0)
192
205
215#define PW_TOKENIZER_REPLACE_FORMAT_STRING(...) \
216 _PW_TOKENIZER_REPLACE_FORMAT_STRING(PW_EMPTY_ARGS(__VA_ARGS__), __VA_ARGS__)
217
218#define _PW_TOKENIZER_REPLACE_FORMAT_STRING(empty_args, ...) \
219 _PW_CONCAT_2(_PW_TOKENIZER_REPLACE_FORMAT_STRING_, empty_args)(__VA_ARGS__)
220
221#define _PW_TOKENIZER_REPLACE_FORMAT_STRING_1() _pw_tokenizer_token, 0u
222#define _PW_TOKENIZER_REPLACE_FORMAT_STRING_0(...) \
223 _pw_tokenizer_token, PW_TOKENIZER_ARG_TYPES(__VA_ARGS__), __VA_ARGS__
224
234#define PW_TOKENIZER_ARG_TYPES(...) \
235 PW_DELEGATE_BY_ARG_COUNT(_PW_TOKENIZER_TYPES_, __VA_ARGS__)
236
237PW_EXTERN_C_START
238
239// These functions encode the tokenized strings. These should not be called
240// directly. Instead, use the corresponding PW_TOKENIZE_TO_* macros above.
241void _pw_tokenizer_ToBuffer(void* buffer,
242 size_t* buffer_size_bytes, // input and output arg
243 pw_tokenizer_Token token,
244 pw_tokenizer_ArgTypes types,
245 ...);
246
247// This empty function allows the compiler to check the format string.
248static inline void pw_tokenizer_CheckFormatString(const char* format, ...)
249 PW_PRINTF_FORMAT(1, 2);
250
251static inline void pw_tokenizer_CheckFormatString(const char* format, ...) {
252 (void)format;
253}
254
255PW_EXTERN_C_END
256
269// clang-format off
270#define PW_TOKENIZE_FORMAT_STRING(domain, mask, format, ...) \
271 static_assert( \
272 PW_FUNCTION_ARG_COUNT(__VA_ARGS__) <= PW_TOKENIZER_MAX_SUPPORTED_ARGS, \
273 "Tokenized strings cannot have more than " \
274 PW_STRINGIFY(PW_TOKENIZER_MAX_SUPPORTED_ARGS) " arguments; " \
275 PW_STRINGIFY(PW_FUNCTION_ARG_COUNT(__VA_ARGS__)) \
276 " arguments were used for " #format " (" #__VA_ARGS__ ")"); \
277 PW_TOKENIZE_FORMAT_STRING_ANY_ARG_COUNT(domain, mask, format, __VA_ARGS__)
278// clang-format on
279
295#define PW_TOKENIZE_FORMAT_STRING_ANY_ARG_COUNT(domain, mask, format, ...) \
296 if (0) { /* Do not execute to prevent double evaluation of the arguments. */ \
297 pw_tokenizer_CheckFormatString(format PW_COMMA_ARGS(__VA_ARGS__)); \
298 } \
299 \
300 _PW_TOKENIZE_VALIDATE_FORMAT_STRING(format); \
301 \
302 /* Tokenize the string to a pw_tokenizer_Token at compile time. */ \
303 static _PW_TOKENIZER_CONST pw_tokenizer_Token _pw_tokenizer_token = \
304 _PW_TOKENIZER_MASK_TOKEN(mask, format); \
305 \
306 PW_TOKENIZER_DEFINE_TOKEN(_pw_tokenizer_token, domain, format)
307
308// Creates unique names to use for tokenized string entries and linker sections.
309#define _PW_TOKENIZER_UNIQUE(prefix) PW_CONCAT(prefix, __LINE__, _, __COUNTER__)
310
311#ifdef __cplusplus
312
313#define _PW_TOKENIZER_CONST constexpr
314
321#define PW_TOKENIZER_DEFINE_TOKEN(token, domain, string) \
322 static_assert(::pw::tokenizer::internal::ValidDomain(domain), \
323 "pw_tokenizer domains may only contain alphanumeric " \
324 "characters, underscore, or colon, and cannot start with a " \
325 "number; space characters are ignored"); \
326 alignas(1) static constexpr auto _PW_TOKENIZER_SECTION _PW_TOKENIZER_UNIQUE( \
327 _pw_tokenizer_string_entry_) = \
328 ::pw::tokenizer::internal::MakeEntry(token, domain, string)
329
330// Validates the format string provided to PW_TOKENIZE_FORMAT_STRING and
331// friends.
332#define _PW_TOKENIZE_VALIDATE_FORMAT_STRING(format) \
333 do { \
334 static_assert(!::pw::tokenizer::internal::Contains(format, "%.*s"), \
335 "The %.*s specifier is not supported." \
336 " See https://pwbug.dev/408040194"); \
337 } while (0)
338
339namespace pw::tokenizer {
340
341using Token = ::pw_tokenizer_Token;
342inline constexpr const char* kDefaultDomain = PW_TOKENIZER_DEFAULT_DOMAIN;
343
344namespace internal {
345
346constexpr bool Contains(const char* haystack, const char* needle) {
347 std::string_view haystack_view(haystack);
348 return haystack_view.find(needle) != std::string_view::npos;
349}
350
351} // namespace internal
352} // namespace pw::tokenizer
353
354#else
355
356#define _PW_TOKENIZER_CONST const
357#define _PW_ALIGNAS(alignment) __attribute__((aligned(alignment)))
358
359#define PW_TOKENIZER_DEFINE_TOKEN(token, domain, string) \
360 _PW_ALIGNAS(1) static const _PW_TOKENIZER_STRING_ENTRY(token, domain, string)
361
362// There is no way to do this in C.
363#define _PW_TOKENIZE_VALIDATE_FORMAT_STRING(format) \
364 do { \
365 } while (0)
366
367#endif // __cplusplus
368
369// _PW_TOKENIZER_SECTION places the tokenized strings in a special .pw_tokenizer
370// linker section. Host-side decoding tools read the strings and tokens from
371// this section to build a database of tokenized strings.
372//
373// This section should be declared as type INFO so that it is excluded from the
374// final binary. To declare the section, as well as the .pw_tokenizer.info
375// metadata section, add the following to the linker script's SECTIONS command:
376//
377// .pw_tokenizer.info 0x0 (INFO) :
378// {
379// KEEP(*(.pw_tokenizer.info))
380// }
381//
382// .pw_tokenizer.entries 0x0 (INFO) :
383// {
384// KEEP(*(.pw_tokenizer.entries.*))
385// }
386//
387// A linker script snippet that provides these sections is provided in the file
388// pw_tokenizer_linker_sections.ld. This file may be directly included into
389// existing linker scripts.
390//
391// The tokenized string sections can also be managed without linker script
392// modifications, though this is not recommended. The section can be extracted
393// and removed from the ELF with objcopy:
394//
395// objcopy --only-section .pw_tokenizer.* <ORIGINAL_ELF> <OUTPUT_ELF>
396// objcopy --remove-section .pw_tokenizer.* <ORIGINAL_ELF>
397//
398// OUTPUT_ELF will be an ELF with only the tokenized strings, and the original
399// ELF file will have the sections removed.
400//
401// Without the above linker script modifications, the section garbage collection
402// option (--gc-sections) removes the tokenized string sections. To avoid
403// editing the target linker script, a separate metadata ELF can be linked
404// without --gc-sections to preserve the tokenized data.
405//
406// pw_tokenizer is intended for use with ELF files only. Mach-O files (macOS
407// executables) do not support section names longer than 16 characters, so a
408// short, unused section name is used on macOS.
409#ifdef __APPLE__
410#define _PW_TOKENIZER_SECTION \
411 PW_KEEP_IN_SECTION(PW_STRINGIFY(_PW_TOKENIZER_UNIQUE(.pw.)))
412#else
413#define _PW_TOKENIZER_SECTION \
414 PW_KEEP_IN_SECTION(PW_STRINGIFY(_PW_TOKENIZER_UNIQUE(.pw_tokenizer.entries.)))
415#endif // __APPLE__
#define PW_PRINTF_FORMAT(format_index, parameter_index)
Definition: compiler.h:86