Commits

George Russell committed 3257ae7

Pragmas to enable and disable ConstByDefault - permit mixing normal C++ with ConstByDefault C++

  • Participants
  • Parent commits 0beaa78

Comments (0)

Files changed (4)

File ConstCPPTests/examples2.cpp

+#pragma CONST_CPP OFF
+#include <cstdio>
+
+void sum(int* a, int* b, int* r)
+{
+    *r = *b + *a;   
+}
+
+#pragma CONST_CPP ON
+
+void sum(int* a, int* b, mutable int*  r)
+{
+    *r = *b + *a + 1;   
+}
+
+int main(int argc, char** argv) {
+    // Call normal C++ sum
+    mutable int a = 2,b =2,r = 0;
+    sum(&a,&b,&r);
+    // Call ConstByDefault C++ sum
+    int a2 = 2, b2 = 2;
+    mutable int r2 = 0;
+    sum(&a2,&b2,&r2);
+    
+    printf("r(4) == %d, r2(5) == %d\n",r,r2);
+    
+    return 0;
+}

File tools/clang/include/clang/Lex/Preprocessor.h

   void setDiagnostics(Diagnostic &D) { Diags = &D; }
 
   const LangOptions &getLangOptions() const { return Features; }
+  LangOptions &getMutableLangOptions()  { return Features; }
   const TargetInfo &getTargetInfo() const { return Target; }
   FileManager &getFileManager() const { return FileMgr; }
   SourceManager &getSourceManager() const { return SourceMgr; }

File tools/clang/lib/Lex/Pragma.cpp

   }
 };
 
+/// ConstCPP handler - 
+struct PragmaConstCPPHandler : public PragmaHandler {
+  PragmaConstCPPHandler()
+    : PragmaHandler("CONST_CPP") {}
+  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
+                            Token &Tok) {
+    tok::OnOffSwitch OOS;
+    PP.LexOnOffSwitch(OOS);
+    if (OOS == tok::OOS_ON)
+      PP.getMutableLangOptions().ConstByDefault = 1;
+    if (OOS == tok::OOS_OFF)
+      PP.getMutableLangOptions().ConstByDefault = 0;
+    if (OOS == tok::OOS_DEFAULT)
+      PP.getMutableLangOptions().ConstByDefault = 1;
+  }
+};
+
 /// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
 struct PragmaSTDC_UnknownHandler : public PragmaHandler {
   PragmaSTDC_UnknownHandler() {}
   AddPragmaHandler(new PragmaPopMacroHandler());
   AddPragmaHandler(new PragmaMessageHandler());
 
+  AddPragmaHandler(new PragmaConstCPPHandler());
+
   // #pragma GCC ...
   AddPragmaHandler("GCC", new PragmaPoisonHandler());
   AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());

File tools/clang/test/SemaConstByDefault/negative_test.cpp

 // RUN: %clang_cc1 -fconstbydefault -fsyntax-only -verify %s
 
+struct TTT {
+    int a, b;  
+};
+
+#pragma  CONST_CPP OFF
+
+void normal(int a) {
+    a++; // Mutate!
+    const int b = 1;
+    b++; // expected-error {{read-only variable is not assignable}}
+    mutable int c; // expected-error {{'mutable' can only be applied to member variables}}
+}
+
+void normal2(int * a) {
+    *a = 1;  
+    TTT x = {2,3};
+    x.a = 1; // expected-error {{read-only variable is not assignable}}
+}
+
+#pragma  CONST_CPP ON
+
+void call_normal(int a)
+{
+    normal(a);   
+}
+
+void call_normal2(mutable int a)
+{
+    normal2(&a);   
+}
+
+#pragma  CONST_CPP DEFAULT
+
 void f() 
 {
     int x = 0;