ksaunders created this revision.
ksaunders added a reviewer: rsmith.
ksaunders added a project: clang.
Herald added a project: All.
ksaunders requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.

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: https://9p.io/sys/doc/comp.html. 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: 
https://reviews.llvm.org/D3853.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127462

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/test/Sema/c11-typedef-redef.c


Index: clang/test/Sema/c11-typedef-redef.c
===================================================================
--- clang/test/Sema/c11-typedef-redef.c
+++ clang/test/Sema/c11-typedef-redef.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -std=c11 %s -verify
+// RUN: %clang_cc1 -fplan9-extensions %s -verify
 
 typedef int type;
 typedef type type;
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -2560,8 +2560,8 @@
     return New->setInvalidDecl();
   }
 
-  // Modules always permit redefinition of typedefs, as does C11.
-  if (getLangOpts().Modules || getLangOpts().C11)
+  // Modules always permit redefinition of typedefs, as do C11 and Plan 9.
+  if (getLangOpts().Modules || getLangOpts().C11 || getLangOpts().Plan9)
     return;
 
   // If we have a redefinition of a typedef in C, emit a warning.  This warning
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -6495,6 +6495,9 @@
   Args.addOptInFlag(CmdArgs, options::OPT_fborland_extensions,
                     options::OPT_fno_borland_extensions);
 
+  Args.addOptInFlag(CmdArgs, options::OPT_fplan9_extensions,
+                    options::OPT_fno_plan9_extensions);
+
   // -fno-declspec is default, except for PS4/PS5.
   if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec,
                    RawTriple.isPS()))
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1343,6 +1343,10 @@
   LangOpts<"Borland">, DefaultFalse,
   PosFlag<SetTrue, [CC1Option], "Accept non-standard constructs supported by 
the Borland compiler">,
   NegFlag<SetFalse>>;
+defm plan9_extensions : BoolFOption<"plan9-extensions",
+  LangOpts<"Plan9">, DefaultFalse,
+  PosFlag<SetTrue, [CC1Option], "Accept non-standard constructs supported by 
the Plan 9 compiler">,
+  NegFlag<SetFalse>>;
 def fbuiltin : Flag<["-"], "fbuiltin">, Group<f_Group>, Flags<[CoreOption]>;
 def fbuiltin_module_map : Flag <["-"], "fbuiltin-module-map">, Group<f_Group>,
   Flags<[NoXarchOption]>, HelpText<"Load the clang builtins module map file.">;
Index: clang/include/clang/Basic/LangOptions.def
===================================================================
--- clang/include/clang/Basic/LangOptions.def
+++ clang/include/clang/Basic/LangOptions.def
@@ -92,6 +92,7 @@
 LANGOPT(MicrosoftExt      , 1, 0, "Microsoft C++ extensions")
 LANGOPT(AsmBlocks         , 1, 0, "Microsoft inline asm blocks")
 LANGOPT(Borland           , 1, 0, "Borland extensions")
+LANGOPT(Plan9             , 1, 0, "Plan 9 extensions")
 LANGOPT(CPlusPlus         , 1, 0, "C++")
 LANGOPT(CPlusPlus11       , 1, 0, "C++11")
 LANGOPT(CPlusPlus14       , 1, 0, "C++14")


Index: clang/test/Sema/c11-typedef-redef.c
===================================================================
--- clang/test/Sema/c11-typedef-redef.c
+++ clang/test/Sema/c11-typedef-redef.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -std=c11 %s -verify
+// RUN: %clang_cc1 -fplan9-extensions %s -verify
 
 typedef int type;
 typedef type type;
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -2560,8 +2560,8 @@
     return New->setInvalidDecl();
   }
 
-  // Modules always permit redefinition of typedefs, as does C11.
-  if (getLangOpts().Modules || getLangOpts().C11)
+  // Modules always permit redefinition of typedefs, as do C11 and Plan 9.
+  if (getLangOpts().Modules || getLangOpts().C11 || getLangOpts().Plan9)
     return;
 
   // If we have a redefinition of a typedef in C, emit a warning.  This warning
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -6495,6 +6495,9 @@
   Args.addOptInFlag(CmdArgs, options::OPT_fborland_extensions,
                     options::OPT_fno_borland_extensions);
 
+  Args.addOptInFlag(CmdArgs, options::OPT_fplan9_extensions,
+                    options::OPT_fno_plan9_extensions);
+
   // -fno-declspec is default, except for PS4/PS5.
   if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec,
                    RawTriple.isPS()))
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1343,6 +1343,10 @@
   LangOpts<"Borland">, DefaultFalse,
   PosFlag<SetTrue, [CC1Option], "Accept non-standard constructs supported by the Borland compiler">,
   NegFlag<SetFalse>>;
+defm plan9_extensions : BoolFOption<"plan9-extensions",
+  LangOpts<"Plan9">, DefaultFalse,
+  PosFlag<SetTrue, [CC1Option], "Accept non-standard constructs supported by the Plan 9 compiler">,
+  NegFlag<SetFalse>>;
 def fbuiltin : Flag<["-"], "fbuiltin">, Group<f_Group>, Flags<[CoreOption]>;
 def fbuiltin_module_map : Flag <["-"], "fbuiltin-module-map">, Group<f_Group>,
   Flags<[NoXarchOption]>, HelpText<"Load the clang builtins module map file.">;
Index: clang/include/clang/Basic/LangOptions.def
===================================================================
--- clang/include/clang/Basic/LangOptions.def
+++ clang/include/clang/Basic/LangOptions.def
@@ -92,6 +92,7 @@
 LANGOPT(MicrosoftExt      , 1, 0, "Microsoft C++ extensions")
 LANGOPT(AsmBlocks         , 1, 0, "Microsoft inline asm blocks")
 LANGOPT(Borland           , 1, 0, "Borland extensions")
+LANGOPT(Plan9             , 1, 0, "Plan 9 extensions")
 LANGOPT(CPlusPlus         , 1, 0, "C++")
 LANGOPT(CPlusPlus11       , 1, 0, "C++11")
 LANGOPT(CPlusPlus14       , 1, 0, "C++14")
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to