From: Arnaud Giersch Date: Mon, 23 Dec 2019 21:09:47 +0000 (+0100) Subject: [sonar] Constify pointer and reference local variables in src/xbt/. X-Git-Tag: v3.25~228 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/5c14275ba09456406ec4b8bf1ef2ca76e18fb32e?ds=sidebyside [sonar] Constify pointer and reference local variables in src/xbt/. --- diff --git a/src/xbt/automaton/automaton.c b/src/xbt/automaton/automaton.c index 708b5e1abf..b15cd73487 100644 --- a/src/xbt/automaton/automaton.c +++ b/src/xbt/automaton/automaton.c @@ -316,8 +316,8 @@ int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_ unsigned int nb_elem = xbt_dynar_length(s1); for (unsigned int cursor = 0; cursor < nb_elem; cursor++) { - int* iptr1 = xbt_dynar_get_ptr(s1, cursor); - int* iptr2 = xbt_dynar_get_ptr(s2, cursor); + const int* iptr1 = xbt_dynar_get_ptr(s1, cursor); + const int* iptr2 = xbt_dynar_get_ptr(s2, cursor); if(*iptr1 != *iptr2) return 1; } diff --git a/src/xbt/automaton/automatonparse_promela.c b/src/xbt/automaton/automatonparse_promela.c index bb18110f54..84cb5838e1 100644 --- a/src/xbt/automaton/automatonparse_promela.c +++ b/src/xbt/automaton/automatonparse_promela.c @@ -22,13 +22,13 @@ char* state_id_src; static void new_state(char* id, int src){ char* saveptr = NULL; // for strtok_r() char* id_copy = xbt_strdup(id); - char* first_part = strtok_r(id_copy, "_", &saveptr); + const char* first_part = strtok_r(id_copy, "_", &saveptr); int type = 0 ; // -1=initial state; 0=intermediate state; 1=final state if(strcmp(first_part,"accept")==0){ type = 1; }else{ - char* second_part = strtok_r(NULL, "_", &saveptr); + const char* second_part = strtok_r(NULL, "_", &saveptr); if(strcmp(second_part,"init")==0){ type = -1; } diff --git a/src/xbt/dict.cpp b/src/xbt/dict.cpp index 44361dc7df..1b33417643 100644 --- a/src/xbt/dict.cpp +++ b/src/xbt/dict.cpp @@ -197,7 +197,7 @@ void xbt_dict_set(xbt_dict_t dict, const char* key, void* data) void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len) { unsigned int hash_code = xbt_str_hash_ext(key, key_len); - xbt_dictelm_t current = dict->table[hash_code & dict->table_size]; + const s_xbt_dictelm* current = dict->table[hash_code & dict->table_size]; while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len || memcmp(key, current->key, key_len))) { @@ -214,7 +214,7 @@ void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len) void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len) { unsigned int hash_code = xbt_str_hash_ext(key, key_len); - xbt_dictelm_t current = dict->table[hash_code & dict->table_size]; + const s_xbt_dictelm* current = dict->table[hash_code & dict->table_size]; while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len || memcmp(key, current->key, key_len))) { @@ -235,7 +235,7 @@ void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len) char *xbt_dict_get_key(xbt_dict_t dict, const void *data) { for (int i = 0; i <= dict->table_size; i++) { - xbt_dictelm_t current = dict->table[i]; + const s_xbt_dictelm* current = dict->table[i]; while (current != nullptr) { if (current->content == data) return current->key; @@ -285,7 +285,7 @@ xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key) */ void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key) { - xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key); + const s_xbt_dictelm* current = xbt_dict_get_elm_or_null(dict, key); if (current == nullptr) return nullptr; diff --git a/src/xbt/dict_cursor.c b/src/xbt/dict_cursor.c index 5ee224a727..ca96b1c6bf 100644 --- a/src/xbt/dict_cursor.c +++ b/src/xbt/dict_cursor.c @@ -123,7 +123,7 @@ inline void xbt_dict_cursor_step(xbt_dict_cursor_t cursor) */ inline int xbt_dict_cursor_get_or_free(xbt_dict_cursor_t * cursor, char **key, void **data) { - xbt_dictelm_t current; + const struct s_xbt_dictelm* current; XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_get_or_free"); diff --git a/src/xbt/dict_test.cpp b/src/xbt/dict_test.cpp index 8fe2f65d0c..2045fa4a6e 100644 --- a/src/xbt/dict_test.cpp +++ b/src/xbt/dict_test.cpp @@ -192,7 +192,7 @@ TEST_CASE("xbt::dict: dict data container", "dict") /* RETRIEVE */ INFO("Search 123"); - char* data = (char*)xbt_dict_get(head, "123"); + const char* data = (char*)xbt_dict_get(head, "123"); REQUIRE((data && strcmp("123", data) == 0)); search_not_found(head, "Can't be found"); @@ -299,7 +299,7 @@ TEST_CASE("xbt::dict: dict data container", "dict") INFO("using 1000 elements with " << SIZEOFKEY << " chars long randomized keys."); xbt_dict_t head = xbt_dict_new_homogeneous(free); for (int j = 0; j < 1000; j++) { - char* data = nullptr; + const char* data = nullptr; char* key = (char*)xbt_malloc(SIZEOFKEY); do { diff --git a/src/xbt/dynar.cpp b/src/xbt/dynar.cpp index 5c62e73975..a1e04a8f8e 100644 --- a/src/xbt/dynar.cpp +++ b/src/xbt/dynar.cpp @@ -71,8 +71,7 @@ static inline void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long static inline void _xbt_dynar_get_elm(void *const dst, const xbt_dynar_t dynar, const unsigned long idx) { - void *const elm = _xbt_dynar_elm(dynar, idx); - + const void* const elm = _xbt_dynar_elm(dynar, idx); memcpy(dst, elm, dynar->elmsize); } @@ -167,7 +166,7 @@ void xbt_dynar_merge(xbt_dynar_t* d1, xbt_dynar_t* d2) const unsigned long elmsize = (*d1)->elmsize; - void *ptr = _xbt_dynar_elm((*d2), 0); + const void* ptr = _xbt_dynar_elm((*d2), 0); _xbt_dynar_resize(*d1, (*d1)->size + (*d2)->size); void *elm = _xbt_dynar_elm((*d1), (*d1)->used); diff --git a/src/xbt/dynar_test.cpp b/src/xbt/dynar_test.cpp index d549738fde..ebab878262 100644 --- a/src/xbt/dynar_test.cpp +++ b/src/xbt/dynar_test.cpp @@ -40,7 +40,7 @@ TEST_CASE("xbt::dynar: generic C vector", "dynar") /* 2. Traverse manually the dynar */ for (int i = 0; i < NB_ELEM; i++) { - int* iptr = (int*)xbt_dynar_get_ptr(d, i); + const int* iptr = (int*)xbt_dynar_get_ptr(d, i); REQUIRE(i == *iptr); // The retrieved value is not the same than the injected one } diff --git a/src/xbt/exception.cpp b/src/xbt/exception.cpp index 2d0748a2f5..a152102526 100644 --- a/src/xbt/exception.cpp +++ b/src/xbt/exception.cpp @@ -55,8 +55,7 @@ void log_exception(e_xbt_log_priority_t prio, const char* context, std::exceptio return; try { with_nested->rethrow_nested(); - } - catch (std::exception& nested_exception) { + } catch (const std::exception& nested_exception) { log_exception(prio, "Caused by", nested_exception); } // We could catch nested_exception or WithContextException but we don't bother: @@ -100,14 +99,14 @@ static void handler() } // Parse error are handled differently, as the call stack does not matter, only the file location - catch (simgrid::ParseError& e) { + catch (const simgrid::ParseError& e) { XBT_ERROR("%s", e.what()); XBT_ERROR("Exiting now."); std::abort(); } // We manage C++ exception ourselves - catch (std::exception& e) { + catch (const std::exception& e) { log_exception(xbt_log_priority_critical, "Uncaught exception", e); show_backtrace(bt); std::abort(); diff --git a/src/xbt/log.cpp b/src/xbt/log.cpp index 17cc12c71c..0060b279a1 100644 --- a/src/xbt/log.cpp +++ b/src/xbt/log.cpp @@ -157,7 +157,7 @@ static constexpr size_t XBT_LOG_DYNAMIC_BUFFER_SIZE = 4096; void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...) { - xbt_log_category_t cat = ev->cat; + const xbt_log_category_s* cat = ev->cat; xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden"); xbt_assert(static_cast(ev->priority) < sizeof(xbt_log_priority_names)/sizeof(xbt_log_priority_names[0]), @@ -282,7 +282,7 @@ int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) { std::string res; - xbt_log_category_t cpp = category->parent->firstChild; + const xbt_log_category_s* cpp = category->parent->firstChild; while (cpp) { res += std::string(" ") + cpp->name; cpp = cpp->nextSibling; diff --git a/src/xbt/memory_map.cpp b/src/xbt/memory_map.cpp index 43ce328624..4484dfee55 100644 --- a/src/xbt/memory_map.cpp +++ b/src/xbt/memory_map.cpp @@ -197,7 +197,7 @@ std::vector get_memory_map(pid_t pid) /* Ok we are good enough to try to get the info we need */ /* First get the start and the end address of the map */ - char* tok = strtok_r(lfields[0], "-", &saveptr); + const char* tok = strtok_r(lfields[0], "-", &saveptr); if (tok == nullptr) { std::fprintf(stderr, "Start and end address of the map are not concatenated by a hyphen (-). Recovery impossible.\n"); diff --git a/src/xbt/xbt_os_file.cpp b/src/xbt/xbt_os_file.cpp index ce487f4ebd..5a0b64d9ff 100644 --- a/src/xbt/xbt_os_file.cpp +++ b/src/xbt/xbt_os_file.cpp @@ -25,7 +25,7 @@ simgrid::xbt::Path::Path() { #if HAVE_UNISTD_H char buffer[2048]; - char* ret = getcwd(buffer, 2048); + const char* ret = getcwd(buffer, 2048); xbt_assert(ret == buffer, "Error during getcwd: %s", strerror(errno)); path_ = std::string(buffer); #else @@ -36,13 +36,13 @@ simgrid::xbt::Path::Path() std::string simgrid::xbt::Path::get_dir_name() { std::string p(path_); - char *res = dirname(&p[0]); + const char* res = dirname(&p[0]); return std::string(res, strlen(res)); } std::string simgrid::xbt::Path::get_base_name() { std::string p(path_); - char *res = basename(&p[0]); + const char* res = basename(&p[0]); return std::string(res, strlen(res)); } diff --git a/src/xbt/xbt_replay.cpp b/src/xbt/xbt_replay.cpp index 2c51d9d81a..cb966e9693 100644 --- a/src/xbt/xbt_replay.cpp +++ b/src/xbt/xbt_replay.cpp @@ -124,8 +124,7 @@ int replay_runner(const char* actor_name, const char* trace_filename) delete evt; } if (action_queues.find(actor_name_string) != action_queues.end()) { - std::queue* myqueue = action_queues.at(actor_name_string); - delete myqueue; + delete action_queues.at(actor_name_string); action_queues.erase(actor_name_string); } } else { // Should have got my trace file in argument diff --git a/src/xbt/xbt_virtu.cpp b/src/xbt/xbt_virtu.cpp index 557a2d3e03..e58f4fddf8 100644 --- a/src/xbt/xbt_virtu.cpp +++ b/src/xbt/xbt_virtu.cpp @@ -11,7 +11,7 @@ int xbt_getpid() { - smx_actor_t self = SIMIX_process_self(); + const simgrid::kernel::actor::ActorImpl* self = SIMIX_process_self(); return self == nullptr ? 0 : self->get_pid(); }