Skip to content

Commit d0968a1

Browse files
IOBYTEdanmar
authored andcommitted
Warn about unknown macro causing template syntax error (#2222)
1 parent cd816f4 commit d0968a1

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎lib/tokenize.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9405,8 +9405,12 @@ void Tokenizer::findGarbageCode() const
94059405
for (const Token *tok = tokens(); tok; tok = tok->next()) {
94069406
if (!Token::simpleMatch(tok, "template <"))
94079407
continue;
9408-
if (tok->previous() && !Token::Match(tok->previous(), "[:;{})>]"))
9409-
syntaxError(tok);
9408+
if (tok->previous() && !Token::Match(tok->previous(), "[:;{})>]")) {
9409+
if (tok->previous()->isUpperCaseName())
9410+
unknownMacroError(tok->previous());
9411+
else
9412+
syntaxError(tok);
9413+
}
94109414
const Token * const tok1 = tok;
94119415
tok = tok->next()->findClosingBracket();
94129416
if (!tok)

‎test/testsimplifytemplate.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ class TestSimplifyTemplate : public TestFixture {
186186
TEST_CASE(template146); // syntax error
187187
TEST_CASE(template147); // syntax error
188188
TEST_CASE(template148); // syntax error
189+
TEST_CASE(template149); // unknown macro
189190
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
190191
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
191192
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@@ -3543,6 +3544,13 @@ class TestSimplifyTemplate : public TestFixture {
35433544
ASSERT_EQUALS(exp, tok(code));
35443545
}
35453546

3547+
void template149() { // unknown macro
3548+
const char code[] = "BEGIN_VERSIONED_NAMESPACE_DECL\n"
3549+
"template<typename T> class Fred { };\n"
3550+
"END_VERSIONED_NAMESPACE_DECL";
3551+
ASSERT_THROW_EQUALS(tok(code), InternalError, "There is an unknown macro here somewhere. Configuration is required. If BEGIN_VERSIONED_NAMESPACE_DECL is a macro then please configure it.");
3552+
}
3553+
35463554
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
35473555
const char code[] = "template <typename T> struct C {};\n"
35483556
"template <typename T> struct S {a};\n"

‎test/testsuite.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ extern std::ostringstream output;
104104
#define ASSERT_EQUALS_DOUBLE( EXPECTED , ACTUAL, TOLERANCE ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL, TOLERANCE)
105105
#define ASSERT_EQUALS_MSG( EXPECTED , ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
106106
#define ASSERT_THROW( CMD, EXCEPTION ) try { CMD ; assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&) { } catch (...) { assertThrowFail(__FILE__, __LINE__); }
107+
#define ASSERT_THROW_EQUALS( CMD, EXCEPTION, EXPECTED ) try { CMD ; assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { assertEquals(__FILE__, __LINE__, EXPECTED, e.errorMessage); } catch (...) { assertThrowFail(__FILE__, __LINE__); }
107108
#define ASSERT_NO_THROW( CMD ) try { CMD ; } catch (...) { assertNoThrowFail(__FILE__, __LINE__); }
108109
#define TODO_ASSERT_THROW( CMD, EXCEPTION ) try { CMD ; } catch (const EXCEPTION&) { } catch (...) { assertThrow(__FILE__, __LINE__); }
109110
#define TODO_ASSERT( CONDITION ) { const bool condition=(CONDITION); todoAssertEquals(__FILE__, __LINE__, true, false, condition); }

0 commit comments

Comments
 (0)