Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
Trac
Trac
  • Project overview
    • Project overview
    • Details
    • Activity
  • Issues 246
    • Issues 246
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Operations
    • Operations
    • Metrics
    • Incidents
  • Analytics
    • Analytics
    • Value Stream
  • Wiki
    • Wiki
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Create a new issue
  • Issue Boards

GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

  • Legacy
  • TracTrac
  • Issues
  • #28193

Closed (moved)
Open
Opened Oct 25, 2018 by riastradh@riastradh

Compile-time assertion

The enclosed patch implements a CTASSERT(condition) macro that, at the top level of a file, causes a compiler error if the constant expression condition evaluates to false. This is conciser than

#if !condition
#error Condition was false.
#endif

and applicable in situations that #if cannot handle, because CTASSERT allows any constant expressions, including, e.g., sizeof, while #if is limited to C preprocessor conditional expansion. (Conversely, CTASSERT can't be used with defined(...), so it does not subsume #if.)

nickm suggested that it should be in src/lib/cc, so I put it there. If you think it should be in a different file, go for it.

The patch uses a couple of different mechanisms to implement it, depending on what the compiler supports:

  • If C11 is available, it expands to _Static_assert(condition, #condition). Obviously if you have a C11 compiler this is the best way to do it, because it is most likely to give the best error message.
  • If any of __COUNTER__, or __INCLUDE_LEVEL__ and ___LINE__, or just __LINE__, is available, their macro values are expanded and appended to a name tor_ctassert_ which is typedef'd to an array type with negative length if the condition is false, and positive length if the condition is true. This has zero run-time overhead; the use of __COUNTER__, &c., is to attain a unique name, which is guaranteed with __COUNTER__, and highly likely with __INCLUDE_LEVEL__ and __LINE__.
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Tor: 0.4.0.x-final
Milestone
Tor: 0.4.0.x-final
Assign milestone
Time tracking
None
Due date
None
Reference: legacy/trac#28193