Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce scope for static variables.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 6 May 2022 08:17:57 +0000 (10:17 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 6 May 2022 08:17:57 +0000 (10:17 +0200)
Use the init-statement to declare variables inside the if statement (sonar).

src/kernel/actor/SimcallObserver.cpp
src/mc/inspect/DwarfExpression.cpp
src/s4u/s4u_Engine.cpp
src/surf/network_ns3.cpp
src/xbt/exception.cpp

index 1d402ab..b4422d5 100644 (file)
@@ -41,8 +41,7 @@ int RandomSimcall::get_max_consider()
 
 bool ConditionWaitSimcall::is_enabled()
 {
-  static bool warned = false;
-  if (not warned) {
+  if (static bool warned = false; not warned) {
     XBT_INFO("Using condition variables in model-checked code is still experimental. Use at your own risk");
     warned = true;
   }
index 0a4f430..0963f8a 100644 (file)
@@ -161,13 +161,13 @@ void execute(const Dwarf_Op* ops, std::size_t n, const ExpressionContext& contex
       default:
 
         // Registers:
-        static const std::unordered_set<uint8_t> registers = {
-            DW_OP_breg0,  DW_OP_breg1,  DW_OP_breg2,  DW_OP_breg3,  DW_OP_breg4,  DW_OP_breg5,  DW_OP_breg6,
-            DW_OP_breg7,  DW_OP_breg8,  DW_OP_breg9,  DW_OP_breg10, DW_OP_breg11, DW_OP_breg12, DW_OP_breg13,
-            DW_OP_breg14, DW_OP_breg15, DW_OP_breg16, DW_OP_breg17, DW_OP_breg18, DW_OP_breg19, DW_OP_breg20,
-            DW_OP_breg21, DW_OP_breg22, DW_OP_breg23, DW_OP_breg24, DW_OP_breg25, DW_OP_breg26, DW_OP_breg27,
-            DW_OP_breg28, DW_OP_breg29, DW_OP_breg30, DW_OP_breg31};
-        if (registers.count(atom) > 0) {
+        if (static const std::unordered_set<uint8_t> registers =
+                {DW_OP_breg0,  DW_OP_breg1,  DW_OP_breg2,  DW_OP_breg3,  DW_OP_breg4,  DW_OP_breg5,  DW_OP_breg6,
+                 DW_OP_breg7,  DW_OP_breg8,  DW_OP_breg9,  DW_OP_breg10, DW_OP_breg11, DW_OP_breg12, DW_OP_breg13,
+                 DW_OP_breg14, DW_OP_breg15, DW_OP_breg16, DW_OP_breg17, DW_OP_breg18, DW_OP_breg19, DW_OP_breg20,
+                 DW_OP_breg21, DW_OP_breg22, DW_OP_breg23, DW_OP_breg24, DW_OP_breg25, DW_OP_breg26, DW_OP_breg27,
+                 DW_OP_breg28, DW_OP_breg29, DW_OP_breg30, DW_OP_breg31};
+            registers.count(atom) > 0) {
           // Push register + constant:
           int register_id = simgrid::dwarf::dwarf_register_to_libunwind(op->atom - DW_OP_breg0);
           unw_word_t res;
@@ -181,22 +181,26 @@ void execute(const Dwarf_Op* ops, std::size_t n, const ExpressionContext& contex
         // ***** Constants:
 
         // Short constant literals:
-        static const std::unordered_set<uint8_t> literals = {
-            DW_OP_lit0,  DW_OP_lit1,  DW_OP_lit2,  DW_OP_lit3,  DW_OP_lit4,  DW_OP_lit5,  DW_OP_lit6,  DW_OP_lit7,
-            DW_OP_lit8,  DW_OP_lit9,  DW_OP_lit10, DW_OP_lit11, DW_OP_lit12, DW_OP_lit13, DW_OP_lit14, DW_OP_lit15,
-            DW_OP_lit16, DW_OP_lit17, DW_OP_lit18, DW_OP_lit19, DW_OP_lit20, DW_OP_lit21, DW_OP_lit22, DW_OP_lit23,
-            DW_OP_lit24, DW_OP_lit25, DW_OP_lit26, DW_OP_lit27, DW_OP_lit28, DW_OP_lit29, DW_OP_lit30, DW_OP_lit31};
-        if (literals.count(atom) > 0) {
+        if (static const std::unordered_set<uint8_t> literals = {DW_OP_lit0,  DW_OP_lit1,  DW_OP_lit2,  DW_OP_lit3,
+                                                                 DW_OP_lit4,  DW_OP_lit5,  DW_OP_lit6,  DW_OP_lit7,
+                                                                 DW_OP_lit8,  DW_OP_lit9,  DW_OP_lit10, DW_OP_lit11,
+                                                                 DW_OP_lit12, DW_OP_lit13, DW_OP_lit14, DW_OP_lit15,
+                                                                 DW_OP_lit16, DW_OP_lit17, DW_OP_lit18, DW_OP_lit19,
+                                                                 DW_OP_lit20, DW_OP_lit21, DW_OP_lit22, DW_OP_lit23,
+                                                                 DW_OP_lit24, DW_OP_lit25, DW_OP_lit26, DW_OP_lit27,
+                                                                 DW_OP_lit28, DW_OP_lit29, DW_OP_lit30, DW_OP_lit31};
+            literals.count(atom) > 0) {
           // Push a literal/constant on the stack:
           stack.push(atom - DW_OP_lit0);
           break;
         }
 
         // General constants:
-        static const std::unordered_set<uint8_t> constants = {
-            DW_OP_const1u, DW_OP_const2u, DW_OP_const4u, DW_OP_const8u, DW_OP_const1s,
-            DW_OP_const2s, DW_OP_const4s, DW_OP_const8s, DW_OP_constu,  DW_OP_consts};
-        if (constants.count(atom) > 0) {
+        if (static const std::unordered_set<uint8_t> constants = {DW_OP_const1u, DW_OP_const2u, DW_OP_const4u,
+                                                                  DW_OP_const8u, DW_OP_const1s, DW_OP_const2s,
+                                                                  DW_OP_const4s, DW_OP_const8s, DW_OP_constu,
+                                                                  DW_OP_consts};
+            constants.count(atom) > 0) {
           // Push the constant argument on the stack.
           stack.push(op->number);
           break;
index 4381569..d0160a0 100644 (file)
@@ -339,8 +339,7 @@ void Engine::run() const
 }
 void Engine::run_until(double max_date) const
 {
-  static bool callback_called = false;
-  if (not callback_called) {
+  if (static bool callback_called = false; not callback_called) {
     on_simulation_start();
     callback_called = true;
   }
index 26bb190..a3ad27a 100644 (file)
@@ -505,8 +505,7 @@ NetworkNS3Action::NetworkNS3Action(Model* model, double totalBytes, s4u::Host* s
   // ns-3 fails when src = dst, so avoid the problem by considering that communications are infinitely fast on the
   // loopback that does not exists
   if (src == dst) {
-    static bool warned = false;
-    if (not warned) {
+    if (static bool warned = false; not warned) {
       XBT_WARN("Sending from a host %s to itself is not supported by ns-3. Every such communication finishes "
                "immediately upon startup.",
                src->get_cname());
index bcc183b..bf5caf1 100644 (file)
@@ -81,8 +81,7 @@ static std::terminate_handler previous_terminate_handler = nullptr;
 XBT_ATTRIB_NORETURN static void handler()
 {
   // Avoid doing crazy things if we get an uncaught exception inside an uncaught exception
-  static std::atomic_flag lock = ATOMIC_FLAG_INIT;
-  if (lock.test_and_set()) {
+  if (static std::atomic_flag lock = ATOMIC_FLAG_INIT; lock.test_and_set()) {
     XBT_ERROR("Handling an exception raised an exception. Bailing out.");
     std::abort();
   }