cppparser: Parse macro directly following string literal

This allows the following C code to parse:

    #define SUFFIX "bar"
    const char *str = "foo"SUFFIX;

This is technically not valid C++ since C++ uses this syntax for custom string literals, but we might as well check if there is a macro defined with this name if we can't find a matching string literal and are about to throw an error
This commit is contained in:
rdb 2024-03-28 21:07:09 +01:00
parent ae8d643907
commit e29b326dd8

View File

@ -2186,6 +2186,17 @@ get_literal(int token, YYLTYPE loc, const string &str, const YYSTYPE &value) {
CPPIdentifier *ident = new CPPIdentifier("operator \"\" " + suffix);
CPPDeclaration *decl = ident->find_symbol(current_scope, global_scope, this);
if (decl == nullptr) {
// Special case to handle C code, which allows a macro to directly follow
// a string, like "str"SUFFIX. In this case, it becomes a separate token.
Manifests::const_iterator mi = _manifests.find(suffix);
if (mi != _manifests.end() && !should_ignore_manifest((*mi).second)) {
CPPManifest *manifest = (*mi).second;
_saved_tokens.push_back(expand_manifest(manifest, loc));
return CPPToken(token, loc, str, value);
}
}
if (decl == nullptr || decl->get_subtype() != CPPDeclaration::ST_function_group) {
error("unknown literal suffix " + suffix, loc);
return CPPToken(token, loc, str, value);