Page MenuHomePhabricator

[Clang] Begin implementing Plan 9 C extensions
Needs ReviewPublic

Authored by ksaunders on Jun 9 2022, 5:55 PM.



This patch enables the addition of extensions supported by the Plan 9 C compilers by adding the -fplan9-extensions flag.

This flag currently enables 1 extension: allowing typedefs to be declared multiple times like in C11. Once merged, I have plans to implement the following Plan 9 C compiler behavior, as covered below.

Plan 9 C compilers can be summarized as a C89 compiler with the following non-standard extensions enabled:

  • Embedded structures, like Microsoft C (already implemented in Clang with -fms-extensions)
  • Automatic embedded structure type decay
  • Embedded structure type name accesses

As well as the following standardized C extensions enabled:

  • C99 compound literals
  • C99 designated initializers for arrays and structures
  • C11 redeclaration of typedefs
  • C11 anonymous structures and unions
  • C2x omitting the parameter name in a function definition

A description of these extensions can be found in the How to Use the Plan 9 C Compiler paper by Rob Pike: However, there are no plans to implement the "extern register" feature, which is used in the kernel.

The motivation for this patch, and the patches that follow, are to enable the compilation of the Plan 9 kernel C source and Plan 9 userspace applications with Clang for the purpose of increased code optimizations and sanitizer instrumentation. In this respect, GCC support is inadequate, as it does not support the member resolution algorithm used in the Plan 9 C compilers to reconcile overlapping declaration names in a record.

This patch is largely based off of the following obsolete patch by @pcc:

Diff Detail

Event Timeline

ksaunders created this revision.Jun 9 2022, 5:55 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 9 2022, 5:55 PM
ksaunders requested review of this revision.Jun 9 2022, 5:55 PM
ksaunders retitled this revision from Begin implementing Plan 9 C extensions [V2] to [Clang] Begin implementing Plan 9 C extensions.Jun 9 2022, 5:57 PM
ychen added a subscriber: ychen.Jun 9 2022, 6:17 PM

Two questions for Clang developers as I work on my next patches:

  1. What is the Clang policy for warnings on extension usages? For example, this diff permits redeclaration of typedefs, which is a Plan 9 and Microsoft C extension. Earlier in the file this extension is enabled for Microsoft C as well, however no warning is emitted. In contrast, in Microsoft C if you use an anonymous embedded record in Clang a warning is emitted for extension usage. So it's unclear what should be a warning and what shouldn't be.
  2. MSVC Compatibility has its own dedicated page on the Clang documentation site. Should I add a page for Plan 9 Compatibility which details the status of each feature now (and can be updated in subsequent diffs)?

Gentle ping. I have 2 other patches ready, and I'd like to get the discussion started on this if possible.