Skip to content

Commit 5d9ba16

Browse files
committed
review
1 parent ca6cd8d commit 5d9ba16

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ namespace simplecpp {
18721872

18731873
class Macro {
18741874
public:
1875-
/** a usage of this macro: where it is used and, for INTn_C / UINTn_C, the argument as written in the call */
1875+
/** a usage of this macro: where it is used and, for INTn_C / UINTn_C, the argument of the call */
18761876
struct Usage {
18771877
explicit Usage(const Location &location) : location(location) {}
18781878
Location location;
@@ -2271,18 +2271,19 @@ namespace simplecpp {
22712271
return parametertokens;
22722272
}
22732273

2274-
/** Is name one of the <stdint.h> integer constant macros INTn_C / UINTn_C, n = 8, 16, 32 or 64? */
2274+
/** Is name one of the <stdint.h> integer constant macros INTn_C / UINTn_C, n = 8, 16, 32, 64 or MAX? */
22752275
static bool isIntegerConstantMacro(const std::string &name) {
22762276
const std::string::size_type pos = (name.compare(0, 1, "U") == 0) ? 1 : 0;
22772277
if (name.compare(pos, 3, "INT") != 0)
22782278
return false;
22792279
const std::string bits = name.substr(pos + 3);
2280-
return bits == "8_C" || bits == "16_C" || bits == "32_C" || bits == "64_C";
2280+
return bits == "8_C" || bits == "16_C" || bits == "32_C" || bits == "64_C" || bits == "MAX_C";
22812281
}
22822282

22832283
/**
2284-
* The text between the parentheses of the call as written: the tokens joined with a single space,
2285-
* comments are skipped. nameTokInst is followed by '('.
2284+
* The text between the parentheses of the call: the tokens joined with a single space, comments are
2285+
* skipped. nameTokInst is followed by '('. If the call is in a macro body, the parameters of the
2286+
* enclosing macro have already been substituted and the macros in them expanded.
22862287
*/
22872288
static std::string writtenArgument(const Token *nameTokInst, bool calledInDefine) {
22882289
std::string ret;

‎simplecpp.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,10 @@ namespace simplecpp {
464464
Location macroLocation;
465465
Location useLocation;
466466
bool macroValueKnown;
467-
/** The argument of a call of INTn_C / UINTn_C (n = 8, 16, 32, 64) as written in the call: the tokens
468-
* between the parentheses joined with a single space, comments are skipped. Empty for all other macros. */
467+
/** The argument of a call of INTn_C / UINTn_C (n = 8, 16, 32, 64, MAX): the tokens between the parentheses
468+
* joined with a single space, comments are skipped. Empty for all other macros.
469+
* For a call in the code this is the argument as written. For a call in a macro body it is the argument
470+
* after the parameters of the enclosing macro are substituted and the macros in them are expanded. */
469471
std::string arg;
470472
};
471473

‎test.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4860,6 +4860,27 @@ static void macroUsageArg5()
48604860
"__UINT32_C use 7:1: []\n", macroUsageArg(code));
48614861
}
48624862

4863+
static void macroUsageArg7()
4864+
{
4865+
// INTMAX_C / UINTMAX_C
4866+
ASSERT_EQUALS("INTMAX_C use 2:1: [300]\n", macroUsageArg("#define INTMAX_C(x) x\nINTMAX_C(300)\n"));
4867+
ASSERT_EQUALS("UINTMAX_C use 2:1: [300]\n", macroUsageArg("#define UINTMAX_C(x) x\nUINTMAX_C(300)\n"));
4868+
ASSERT_EQUALS("INTMAXX_C use 2:1: []\n", macroUsageArg("#define INTMAXX_C(x) x\nINTMAXX_C(300)\n"));
4869+
}
4870+
4871+
static void macroUsageArg6()
4872+
{
4873+
// when the call is in a macro body the argument is recorded after the parameters
4874+
// are substituted and the macros in the arguments are expanded
4875+
const char code[] = "#define UINT32_C(x) x\n"
4876+
"#define X(a) UINT32_C(a)\n"
4877+
"#define R(a) a\n"
4878+
"X(R(7) /* c */ + 8)\n";
4879+
ASSERT_EQUALS("R use 4:1: []\n"
4880+
"UINT32_C use 4:1: [7 + 8]\n"
4881+
"X use 4:1: []\n", macroUsageArg(code));
4882+
}
4883+
48634884
static void undef()
48644885
{
48654886
const char code[] = "#define A\n"
@@ -5941,6 +5962,8 @@ static void runTests(int argc, char **argv, Input input)
59415962
TEST_CASE(macroUsageArg3);
59425963
TEST_CASE(macroUsageArg4);
59435964
TEST_CASE(macroUsageArg5);
5965+
TEST_CASE(macroUsageArg6);
5966+
TEST_CASE(macroUsageArg7);
59445967

59455968
TEST_CASE(undef);
59465969

0 commit comments

Comments
 (0)