Skip to content

Commit d3c9a69

Browse files
dmcppcheckclaude
andcommitted
Add Macro::copyMacroCall() for copying a macro call to one line
No behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 953f255 commit d3c9a69

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,34 @@ namespace simplecpp {
19131913
return valueDefinedInCode_;
19141914
}
19151915

1916+
/**
1917+
* Copy a macro call "NAME( ... )" to a tokenlist with no line breaks:
1918+
* all tokens get the location of the name.
1919+
* @param dest destination tokenlist
1920+
* @param nameTok the macro name, followed by '('
1921+
* @return the token after the ')', or nullptr if the call is not terminated
1922+
* @throws invalidDirectiveAsMacroParameter if there is a preprocessor directive in the arguments
1923+
*/
1924+
static const Token *copyMacroCall(TokenList &dest, const Token *nameTok) {
1925+
const Location &loc = nameTok->location;
1926+
dest.push_back(new Token(nameTok->str(), loc, nameTok->whitespaceahead));
1927+
const Token *tok = nameTok->next;
1928+
dest.push_back(new Token(tok->str(), loc, tok->whitespaceahead));
1929+
tok = tok->next;
1930+
int par = 1;
1931+
while (tok && par > 0) {
1932+
if (tok->op == '(')
1933+
++par;
1934+
else if (tok->op == ')')
1935+
--par;
1936+
else if (tok->op == '#' && !sameline(tok->previous, tok))
1937+
throw invalidDirectiveAsMacroParameter(tok->location);
1938+
dest.push_back(new Token(tok->str(), loc, tok->whitespaceahead));
1939+
tok = tok->next;
1940+
}
1941+
return tok;
1942+
}
1943+
19161944
/**
19171945
* Expand macro. This will recursively expand inner macros.
19181946
* @param output destination tokenlist
@@ -1939,24 +1967,9 @@ namespace simplecpp {
19391967
TokenList output2(inputFiles);
19401968

19411969
if (functionLike() && rawtok->next && rawtok->next->op == '(') {
1942-
// Copy macro call to a new tokenlist with no linebreaks
19431970
const Token * const rawtok1 = rawtok;
19441971
TokenList rawtokens2(inputFiles);
1945-
rawtokens2.push_back(new Token(rawtok->str(), rawtok1->location, rawtok->whitespaceahead));
1946-
rawtok = rawtok->next;
1947-
rawtokens2.push_back(new Token(rawtok->str(), rawtok1->location, rawtok->whitespaceahead));
1948-
rawtok = rawtok->next;
1949-
int par = 1;
1950-
while (rawtok && par > 0) {
1951-
if (rawtok->op == '(')
1952-
++par;
1953-
else if (rawtok->op == ')')
1954-
--par;
1955-
else if (rawtok->op == '#' && !sameline(rawtok->previous, rawtok))
1956-
throw invalidDirectiveAsMacroParameter(rawtok->location);
1957-
rawtokens2.push_back(new Token(rawtok->str(), rawtok1->location, rawtok->whitespaceahead));
1958-
rawtok = rawtok->next;
1959-
}
1972+
rawtok = copyMacroCall(rawtokens2, rawtok);
19601973
if (expand(output2, rawtok1->location, rawtokens2.cfront(), macros, expandedmacros, outputList))
19611974
rawtok = rawtok1->next;
19621975
} else {

0 commit comments

Comments
 (0)