[PATCH] D108567: Implement #pragma clang final extension

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 8 08:35:40 PDT 2021


aaron.ballman added inline comments.


================
Comment at: clang/docs/LanguageExtensions.rst:3968-3984
+Final Macros
+============
+
+Clang supports the pragma ``#pragma clang final``, which can be used to
+mark macros as final, meaning they cannot be undef'd or re-defined. For example:
+
+.. code-block:: c
----------------
Design question: would it make sense to extend this slightly so that the macro does not have to be defined in order to be finalized? e.g., this could be used as a way for a library author to say "this identifier cannot be defined as a macro"?


================
Comment at: clang/docs/LanguageExtensions.rst:3979
+   #undef FINAL_MACRO  // warning: FINAL_MACRO is marked final and should not be undefined
+   #define FINAL_MACRO // warning: FINAL_MACRO is marked final and should not be redefined
+
----------------
What happens if the redefinition is to the same token sequence as the original definition? e.g.,
```
#define FINAL_MACRO 1+1
#pragma clang final(FINAL_MACRO)
#define FINAL_MACRO 1+1 // ok?
#define FINAL_MACRO (1+1) // Whoa...slow your roll there, champ!
```


================
Comment at: clang/include/clang/Basic/DiagnosticLexKinds.td:544
+  ExtWarn<"macro %0 has been marked as final and should not be "
+          "%select{un|re}1defined">,
+  InGroup<FinalMacro>;
----------------
Heh, I like your approach, but a goal of %select is to ease translation of our diagnostics to other languages (in theory, anyway).


================
Comment at: clang/include/clang/Basic/IdentifierTable.h:196
+      // If this is a final macro, make the deprecation and header unsafe bits
+      // stick around after the undefinition so they apply to any redefinitions
+      if (!IsFinal) {
----------------



================
Comment at: clang/include/clang/Lex/Preprocessor.h:828
 
-  /// Usage warning for macros marked by #pragma clang restrict_expansion.
-  llvm::DenseMap<const IdentifierInfo *, MsgLocationPair>
-      RestrictExpansionMacroMsgs;
+  /// Warning information for macro annotations
+  llvm::DenseMap<const IdentifierInfo *, MacroAnnotations> AnnotationInfos;
----------------



================
Comment at: clang/lib/Lex/Pragma.cpp:2083
+    if (!II->hasMacroDefinition()) {
+      PP.Diag(Tok, diag::err_pp_visibility_non_macro) << II->getName();
+      return;
----------------
This should cause the macro name to be properly quoted in the diagnostic.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108567/new/

https://reviews.llvm.org/D108567



More information about the cfe-commits mailing list