Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:51

0001 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0002 
0003 // is ok, because const-qualified
0004 const static int g_staticConst = 23;
0005 static int const& g_ref_staticConst = g_staticConst;
0006 static int const* g_ptr_staticConst = &g_staticConst;
0007 
0008 
0009 // results in a warning by GlobalStaticChecker
0010 CMS_THREAD_SAFE static int g_static;
0011 static int * g_ptr_static = &g_static;
0012 
0013 class ClassTest {
0014    public:
0015       explicit ClassTest();
0016       ~ClassTest();
0017 
0018 void testConst() const;
0019 
0020    private:
0021 
0022       mutable int m_testMutable;
0023       int * m_testPointer;
0024       int m_testInteger;
0025 
0026 };
0027 
0028 void
0029 ClassTest::testConst() const
0030 {
0031     // 1) reported by class checker
0032     m_testMutable = 23;
0033 
0034     // 2) compiles, not reported
0035     (*m_testPointer) = 23;
0036 
0037     // 3) compiles, not reported
0038     int * localPtr = m_testPointer;
0039     (*localPtr) = 23;
0040 
0041     // 4) will not compile
0042     // error: invalid conversion from 'const int*' to 'int*'
0043     //int * localPtrToInt = &m_testInteger;
0044 } 
0045 
0046 class Thing
0047 {
0048 private:
0049 
0050 int num;
0051 
0052 public:
0053 
0054 Thing(): num{0} {}
0055 int getnum() {return num;}
0056 void putnum(int x) {num=x;}
0057 };
0058 
0059 class Foo
0060 {
0061 
0062 private:
0063 
0064 int Var_;
0065 int & RVar_=Var_;
0066 int * PVar_=&RVar_;
0067 Thing * T_p;
0068 
0069 public:
0070 
0071 Foo(): Var_{0}{T_p= new Thing;}
0072 void func1(int  x) {return;} //OK
0073 void func2(int &x) {return;} // cound be bad 
0074 void func3(int *x) {return;} // could be bad
0075 void func4(int const *x) {return;} //  OK
0076 void func5(int * const x) {return;} // could be bad
0077 void func6(int const &x) {return;} //OK
0078 void nonConstFunc() { Var_ = 5;}
0079 void constFunc() const { return;}
0080 int * nonConstAccess() const {return PVar_;} //bad
0081 int & nonConstRefAccess() const { return  RVar_; } //bad ?
0082 int const * constAccess() const {return PVar_;} //OK
0083 int const & constRefAccess() const { return RVar_; } //OK ?
0084 Thing * getThing() { return T_p; }
0085 
0086 };
0087 
0088 class Bar
0089 {
0090 CMS_THREAD_SAFE static int si_;
0091 static void const modifyStatic(int &x) {si_=x;}
0092 private:
0093 Bar(): ci_{0},ipc_{&i_},icp_{&i_},ir_{i_},icr_{ci_} {}
0094 const int ci_;
0095 int i_;
0096 int const * icp_;
0097 int * ip_;
0098 int * const ipc_;
0099 int & ir_;
0100 int const & icr_;
0101 Foo foo_;
0102 public:
0103 void modifyMember()  { i_ = 5;}
0104 void indirectModifyMember()  { modifyMember();}
0105 void recursiveCaller(int i) {if (i == 0) return; recursiveCaller(--i);}
0106 
0107 void method1(int &x) {return;}
0108 void method2(const int &x) const {return;}
0109 
0110 void produce() 
0111     {
0112     Foo * foo = new Foo;
0113     int I=0;
0114     int * PI;
0115     int const * CPI;
0116     foo->func1(i_);
0117     foo->func1(ci_);
0118     foo->func1(ir_);
0119     foo->func1(icr_);
0120     foo->func1(I);
0121     foo->func2(i_);
0122     foo->func2(ir_);
0123     foo->func2(I);
0124     foo->func6(i_);
0125     foo->func6(ir_);
0126     foo->func6(I);
0127     foo->nonConstFunc();
0128     foo_.nonConstFunc(); //should fail member data (object) call non const functions 
0129     foo_.constFunc(); //OK because const won't modify self
0130     foo->getThing()->getnum();
0131     foo_.getThing()->getnum();
0132     method1(i_);
0133     method1(I);
0134     modifyStatic(I);
0135     modifyMember();
0136     indirectModifyMember();
0137     recursiveCaller(1);
0138     PI=foo_.nonConstAccess(); //should fail returns pointer to member data that is non const qualified
0139     CPI=foo_.constAccess(); // OK because returns pointer to member data that is const qualified
0140     if (*PI==*CPI) I++;
0141     }
0142 
0143 void method3() const
0144     {
0145     Foo foo;
0146     int I=0;
0147     Bar bar;
0148     foo.func1(i_);
0149     foo.func1(ci_);
0150     foo.func1(ir_);
0151     foo.func1(icr_);
0152     foo.func1(I);
0153     foo.func2(ir_);
0154     foo.func2(I);
0155     foo.func6(i_);
0156     foo.func6(ir_);
0157     foo.func6(I);
0158     foo_.nonConstRefAccess();
0159     foo_.constRefAccess();
0160 //  foo_.nonConstFunc();
0161     foo_.nonConstAccess();
0162     foo_.constAccess();
0163     if (i_) method2(i_);
0164     bar.produce();
0165 // will produce a warning only by ConstCastAwayChecker
0166     int & ir = (int &) (icr_);
0167     int & cir = const_cast<int &>(icr_);
0168     int * ip = (int *) (icp_);
0169     int * cip = const_cast<int *>(icp_);
0170 // must not produce a warning
0171     int const& ira = (int const&)(icr_);
0172 // will produce a warning by StaticLocalChecker
0173         CMS_THREAD_SAFE static int evilStaticLocal = 0;
0174     static int & intRef = evilStaticLocal;
0175     static int * intPtr = & evilStaticLocal;
0176 // no warnings here
0177     static const int c_evilStaticLocal = 0;
0178     static int const& c_intRef = evilStaticLocal;
0179     static int const* c_intPtr = &evilStaticLocal;
0180     static const int * c_intPtr_equivalent = &evilStaticLocal;
0181     static int const* const* c_intPtrPtr = &( c_intPtr);
0182     g_static=23;
0183     si_=23;
0184     modifyStatic(I);
0185     }
0186 
0187 
0188 };
0189 
0190 int main()
0191 {
0192 return 0;
0193 }
0194 
0195 
0196