From b3688f22e8ed7508e027930574e206f3beca20c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 16 Jul 2019 20:32:46 +0200 Subject: [PATCH] Unknown macro: Detect and warn about unknown macro before throw/return --- lib/tokenize.cpp | 6 ++++++ test/testtokenize.cpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 57585efd0..455a08afe 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9127,6 +9127,12 @@ void Tokenizer::findGarbageCode() const unknownMacroError(tok->linkAt(1)->previous()); } + // UNKNOWN_MACRO(return) + for (const Token *tok = tokens(); tok; tok = tok->next()) { + if (Token::Match(tok, "%name% throw|return") && std::isupper(tok->str()[0])) + unknownMacroError(tok); + } + // Assign/increment/decrement literal for (const Token *tok = tokens(); tok; tok = tok->next()) { if (Token::Match(tok, "!!) %num%|%str%|%char% %assign%|++|--")) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 9989df8bb..664d35540 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -469,6 +469,8 @@ private: TEST_CASE(checkConfiguration); TEST_CASE(unknownType); // #8952 + + TEST_CASE(unknownMacroBeforeReturn); } std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Native, const char* filename = "test.cpp", bool cpp11 = true) { @@ -7914,6 +7916,10 @@ private: ASSERT_EQUALS("", errout.str()); } + + void unknownMacroBeforeReturn() { + ASSERT_THROW(tokenizeAndStringify("int f() { X return 0; }"), InternalError); + } }; REGISTER_TEST(TestTokenizer)