From f54a6f085b44e7eda455476d753c4ab82affa68b Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Wed, 3 May 2017 20:36:26 +0200 Subject: [PATCH] #7883 hang: CheckOther::checkFuncArgNamesDifferent() template code in .h. Activate language check for header files + Small refactoring --- lib/tokenize.cpp | 153 ++++++++++++++++++++---------------------- test/testtokenize.cpp | 1 + 2 files changed, 75 insertions(+), 79 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1d9e7b885..a0f7abdce 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7289,16 +7289,14 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr) return false; } -namespace { - const std::set stdFunctionsPresentInC = make_container< std::set > () << - "strcat" << - "strcpy" << - "strncat" << - "strncpy" << - "free" << - "malloc" << - "strdup"; -} +static const std::set stdFunctionsPresentInC = make_container< std::set > () << + "strcat" << + "strcpy" << + "strncat" << + "strncpy" << + "free" << + "malloc" << + "strdup"; void Tokenizer::simplifyStd() { @@ -8053,11 +8051,11 @@ void Tokenizer::checkConfiguration() const void Tokenizer::validateC() const { - if (!isC()) + if (isCPP()) return; for (const Token *tok = tokens(); tok; tok = tok->next()) { // That might trigger false positives, but it's much faster to have this truncated pattern - if (Token::simpleMatch(tok, "const_cast|dynamic_cast|reinterpret_cast|static_cast <")) + if (Token::Match(tok, "const_cast|dynamic_cast|reinterpret_cast|static_cast <")) syntaxErrorC(tok, "C++ cast <..."); // Template function.. if (Token::Match(tok, "%name% < %name% > (")) { @@ -8175,19 +8173,20 @@ const Token * Tokenizer::findGarbageCode() const return nullptr; } +static const std::set controlFlowKeywords = make_container< std::set > () << + "goto" << + "do" << + "if" << + "else" << + "for" << + "while" << + "switch" << + "break" << + "continue" << + "return"; + bool Tokenizer::isGarbageExpr(const Token *start, const Token *end) { - std::set controlFlowKeywords; - controlFlowKeywords.insert("goto"); - controlFlowKeywords.insert("do"); - controlFlowKeywords.insert("if"); - controlFlowKeywords.insert("else"); - controlFlowKeywords.insert("for"); - controlFlowKeywords.insert("while"); - controlFlowKeywords.insert("switch"); - controlFlowKeywords.insert("break"); - controlFlowKeywords.insert("continue"); - controlFlowKeywords.insert("return"); for (const Token *tok = start; tok != end; tok = tok->next()) { if (controlFlowKeywords.find(tok->str()) != controlFlowKeywords.end()) return true; @@ -8647,17 +8646,15 @@ void Tokenizer::simplifyAttribute() } } -namespace { - const std::set keywords = make_container< std::set >() - << "volatile" - << "inline" - << "_inline" - << "__inline" - << "__forceinline" - << "register" - << "__restrict" - << "__restrict__" ; -} +static const std::set keywords = make_container< std::set >() + << "volatile" + << "inline" + << "_inline" + << "__inline" + << "__forceinline" + << "register" + << "__restrict" + << "__restrict__" ; // Remove "volatile", "inline", "register", "restrict", "override", "final", "static" and "constexpr" // "restrict" keyword // - New to 1999 ANSI/ISO C standard @@ -9009,52 +9006,50 @@ void Tokenizer::simplifyBitfields() } -namespace { - // Types and objects in std namespace that are neither functions nor templates - const std::set stdTypes = make_container >() << - "string"<< "wstring"<< "u16string"<< "u32string" << - "iostream"<< "ostream"<< "ofstream"<< "ostringstream" << - "istream"<< "ifstream"<< "istringstream"<< "fstream"<< "stringstream" << - "wstringstream"<< "wistringstream"<< "wostringstream"<< "wstringbuf" << - "stringbuf"<< "streambuf"<< "ios"<< "filebuf"<< "ios_base" << - "exception"<< "bad_exception"<< "bad_alloc" << - "logic_error"<< "domain_error"<< "invalid_argument_"<< "length_error" << - "out_of_range"<< "runtime_error"<< "range_error"<< "overflow_error"<< "underflow_error" << - "locale" << - "cout"<< "cerr"<< "clog"<< "cin" << - "wcerr"<< "wcin"<< "wclog"<< "wcout" << - "endl"<< "ends"<< "flush" << - "boolalpha"<< "noboolalpha"<< "showbase"<< "noshowbase" << - "showpoint"<< "noshowpoint"<< "showpos"<< "noshowpos" << - "skipws"<< "noskipws"<< "unitbuf"<< "nounitbuf"<< "uppercase"<< "nouppercase" << - "dec"<< "hex"<< "oct" << - "fixed"<< "scientific" << - "internal"<< "left"<< "right" << - "fpos"<< "streamoff"<< "streampos"<< "streamsize"; +// Types and objects in std namespace that are neither functions nor templates +static const std::set stdTypes = make_container >() << + "string"<< "wstring"<< "u16string"<< "u32string" << + "iostream"<< "ostream"<< "ofstream"<< "ostringstream" << + "istream"<< "ifstream"<< "istringstream"<< "fstream"<< "stringstream" << + "wstringstream"<< "wistringstream"<< "wostringstream"<< "wstringbuf" << + "stringbuf"<< "streambuf"<< "ios"<< "filebuf"<< "ios_base" << + "exception"<< "bad_exception"<< "bad_alloc" << + "logic_error"<< "domain_error"<< "invalid_argument_"<< "length_error" << + "out_of_range"<< "runtime_error"<< "range_error"<< "overflow_error"<< "underflow_error" << + "locale" << + "cout"<< "cerr"<< "clog"<< "cin" << + "wcerr"<< "wcin"<< "wclog"<< "wcout" << + "endl"<< "ends"<< "flush" << + "boolalpha"<< "noboolalpha"<< "showbase"<< "noshowbase" << + "showpoint"<< "noshowpoint"<< "showpos"<< "noshowpos" << + "skipws"<< "noskipws"<< "unitbuf"<< "nounitbuf"<< "uppercase"<< "nouppercase" << + "dec"<< "hex"<< "oct" << + "fixed"<< "scientific" << + "internal"<< "left"<< "right" << + "fpos"<< "streamoff"<< "streampos"<< "streamsize"; - const std::set stdTemplates = make_container >() << - "array"<< "basic_string"<< "bitset"<< "deque"<< "list"<< "map"<< "multimap" << - "priority_queue"<< "queue"<< "set"<< "multiset"<< "stack"<< "vector"<< "pair" << - "iterator"<< "iterator_traits" << - "unordered_map"<< "unordered_multimap"<< "unordered_set"<< "unordered_multiset" << - "tuple"<< "function"; - const std::set stdFunctions = make_container >() << - "getline" << - "for_each"<< "find"<< "find_if"<< "find_end"<< "find_first_of" << - "adjacent_find"<< "count"<< "count_if"<< "mismatch"<< "equal"<< "search"<< "search_n" << - "copy"<< "copy_backward"<< "swap"<< "swap_ranges"<< "iter_swap"<< "transform"<< "replace" << - "replace_if"<< "replace_copy"<< "replace_copy_if"<< "fill"<< "fill_n"<< "generate"<< "generate_n"<< "remove" << - "remove_if"<< "remove_copy"<< "remove_copy_if" << - "unique"<< "unique_copy"<< "reverse"<< "reverse_copy" << - "rotate"<< "rotate_copy"<< "random_shuffle"<< "partition"<< "stable_partition" << - "sort"<< "stable_sort"<< "partial_sort"<< "partial_sort_copy"<< "nth_element" << - "lower_bound"<< "upper_bound"<< "equal_range"<< "binary_search"<< "merge"<< "inplace_merge"<< "includes" << - "set_union"<< "set_intersection"<< "set_difference" << - "set_symmetric_difference"<< "push_heap"<< "pop_heap"<< "make_heap"<< "sort_heap" << - "min"<< "max"<< "min_element"<< "max_element"<< "lexicographical_compare"<< "next_permutation"<< "prev_permutation" << - "advance"<< "back_inserter"<< "distance"<< "front_inserter"<< "inserter" << - "make_pair"<< "make_shared"<< "make_tuple"; -} +static const std::set stdTemplates = make_container >() << + "array"<< "basic_string"<< "bitset"<< "deque"<< "list"<< "map"<< "multimap" << + "priority_queue"<< "queue"<< "set"<< "multiset"<< "stack"<< "vector"<< "pair" << + "iterator"<< "iterator_traits" << + "unordered_map"<< "unordered_multimap"<< "unordered_set"<< "unordered_multiset" << + "tuple"<< "function"; +static const std::set stdFunctions = make_container >() << + "getline" << + "for_each"<< "find"<< "find_if"<< "find_end"<< "find_first_of" << + "adjacent_find"<< "count"<< "count_if"<< "mismatch"<< "equal"<< "search"<< "search_n" << + "copy"<< "copy_backward"<< "swap"<< "swap_ranges"<< "iter_swap"<< "transform"<< "replace" << + "replace_if"<< "replace_copy"<< "replace_copy_if"<< "fill"<< "fill_n"<< "generate"<< "generate_n"<< "remove" << + "remove_if"<< "remove_copy"<< "remove_copy_if" << + "unique"<< "unique_copy"<< "reverse"<< "reverse_copy" << + "rotate"<< "rotate_copy"<< "random_shuffle"<< "partition"<< "stable_partition" << + "sort"<< "stable_sort"<< "partial_sort"<< "partial_sort_copy"<< "nth_element" << + "lower_bound"<< "upper_bound"<< "equal_range"<< "binary_search"<< "merge"<< "inplace_merge"<< "includes" << + "set_union"<< "set_intersection"<< "set_difference" << + "set_symmetric_difference"<< "push_heap"<< "pop_heap"<< "make_heap"<< "sort_heap" << + "min"<< "max"<< "min_element"<< "max_element"<< "lexicographical_compare"<< "next_permutation"<< "prev_permutation" << + "advance"<< "back_inserter"<< "distance"<< "front_inserter"<< "inserter" << + "make_pair"<< "make_shared"<< "make_tuple"; // Add std:: in front of std classes, when using namespace std; was given diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index b5b275918..68f72737f 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -825,6 +825,7 @@ private: ASSERT_THROW(tokenizeAndStringify(";std::map m;",false,false,Settings::Native,"test.c"), InternalError); ASSERT_THROW(tokenizeAndStringify(";template class X { };",false,false,Settings::Native,"test.c"), InternalError); ASSERT_THROW(tokenizeAndStringify("int X() {};",false,false,Settings::Native,"test.c"), InternalError); + ASSERT_THROW(tokenizeAndStringify("void foo(int i) { reinterpret_cast(i) };",false,false,Settings::Native,"test.h"), InternalError); } void syntax_case_default() { // correct syntax