GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix #613: support binary literals in preprocessor checks by ludviggunne · Pull Request #628 · cppcheck-opensource/simplecpp

Fix #613: support binary literals in preprocessor checks #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
ludviggunne wants to merge 7 commits into cppcheck-opensource:master
base: master
Loading
Loading
from ludviggunne:613

Uh oh!

There was an error while loading. Please reload this page.

Draft

Fix #613: support binary literals in preprocessor checks #628

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
0618d9e
Add tests
ludviggunne Feb 24, 2026
bda56f5
Fix #613
ludviggunne Feb 24, 2026
01bd386
run-tests.py: removed fixed `expr_usual_conversions.c` from exclusion…
firewave Jun 22, 2026
f54a1b5
simplecpp.cpp: fixed `readability-container-size-empty` clang-tidy wa…
firewave Jun 22, 2026
5855c28
update selfcheck.sh shebang
ludviggunne Jul 10, 2026
3acf836
skip suffix
ludviggunne Jul 10, 2026
5fb6ef4
update tests
ludviggunne Jul 10, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

1 change: 0 additions & 1 deletion 1 run-tests.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def cleanup(out: str) -> str:
# todo, high priority
'c99-6_10_3_4_p5.c',
'c99-6_10_3_4_p6.c',
'expr_usual_conversions.c', # condition is true: 4U - 30 >= 0
'stdint.c',

# GCC..
Expand Down
2 changes: 1 addition & 1 deletion 2 selfcheck.sh
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

if [ -z "$SIMPLECPP_PATH" ]; then
SIMPLECPP_PATH=.
Expand Down
76 changes: 53 additions & 23 deletions 76 simplecpp.cpp
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ static bool isOct(const std::string &s)
return s.size()>1 && (s[0]=='0') && (s[1] >= '0') && (s[1] < '8');
}

static bool isBin(const std::string &s)
{
return s.size()>2 && s.compare(0,2,"0b")==0;
}

static bool isStringLiteral(const std::string &s)
{
return s.size() > 1 && (s[0]=='\"') && (*s.rbegin()=='\"');
Expand Down Expand Up @@ -120,32 +125,57 @@ static std::string locstring(const simplecpp::Location &loc)
}
#endif

static long long stringToLL(const std::string &s)
{
long long ret;
const bool hex = isHex(s);
const bool oct = isOct(s);
std::istringstream istr(hex ? s.substr(2) : oct ? s.substr(1) : s);
if (hex)
istr >> std::hex;
else if (oct)
istr >> std::oct;
istr >> ret;
return ret;
static unsigned long long stringToULL(const std::string &s, std::size_t offset = 0)
{
unsigned long long base = 10;
if (isHex(s.substr(offset))) {
offset += 2;
base = 16;
} else if (isOct(s.substr(offset))) {
offset += 1;
base = 8;
} else if (isBin(s.substr(offset))) {
offset += 2;
base = 2;
}

unsigned long long result = 0;
std::size_t i;
for (i = offset; i < s.size(); i++) {
const char c = s[i];

unsigned long long d;
if ('0' <= c && c <= '9') {
d = c - '0';
} else if ('a' <= c && c <= 'f') {
d = 10 + c - 'a';
} else if ('A' <= c && c <= 'F') {
d = 10 + c - 'A';
} else {
break;
}

if (d >= base) {
throw std::runtime_error("invalid integer literal");
}

result = base*result + d;
}

for (; i < s.size(); i++) {
if (!std::strchr("ZLUzlu", s[i])) {
throw std::runtime_error("invalid integer literal");
}
}

return result;
}

static unsigned long long stringToULL(const std::string &s)
static long long stringToLL(const std::string &s)
{
unsigned long long ret;
const bool hex = isHex(s);
const bool oct = isOct(s);
std::istringstream istr(hex ? s.substr(2) : oct ? s.substr(1) : s);
if (hex)
istr >> std::hex;
else if (oct)
istr >> std::oct;
istr >> ret;
return ret;
if (!s.empty() && s[0] == '-')
return -stringToULL(s, 1);
return stringToULL(s);
}

static bool endsWith(const std::string &s, const std::string &e)
Expand Down
53 changes: 53 additions & 0 deletions 53 test.cpp
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,58 @@ static void ifUndefFuncStyleMacro()
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, undefined function-like macro invocation: A( ... )\n", toString(outputList));
}

static void testIfIntegerLiteral(const std::string &value, const std::string &literal, bool expectError)
{
const std::string code = "#define A " + value + "\n"
"#if A == " + literal + "\n"
"int x = " + literal + ";\n"
"#endif\n";

simplecpp::OutputList outputList;

std::string literalPreprocessed = literal;
if (literalPreprocessed[0] == '-')
{
literalPreprocessed = "- " + literalPreprocessed.substr(1);
}

if (expectError) {
ASSERT_EQUALS("", preprocess(code.c_str(), &outputList));
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, invalid integer literal\n", toString(outputList));
} else {
ASSERT_EQUALS("\n\nint x = " + literalPreprocessed + " ;", preprocess(code.c_str(), &outputList));
ASSERT_EQUALS("", toString(outputList));
}
}

static void ifIntegerLiteral()
{
testIfIntegerLiteral("123", "123", false);
testIfIntegerLiteral("-123", "-123", false);
testIfIntegerLiteral("123", "123Z", false);
testIfIntegerLiteral("123", "123UL", false);
testIfIntegerLiteral("-123", "-123LL", false);
testIfIntegerLiteral("123", "123z", false);
testIfIntegerLiteral("123", "123ul", false);
testIfIntegerLiteral("-123", "-123ll", false);

testIfIntegerLiteral("123", "0x7b", false);
testIfIntegerLiteral("-123", "-0x7b", false);

testIfIntegerLiteral("123", "0x7B", false);
testIfIntegerLiteral("-123", "-0x7B", false);

testIfIntegerLiteral("123", "0173", false);
testIfIntegerLiteral("-123", "-0173", false);

testIfIntegerLiteral("123", "0b1111011", false);
testIfIntegerLiteral("-123", "-0b1111011", false);

testIfIntegerLiteral("123", "0xGH", true);
testIfIntegerLiteral("123", "019", true);
testIfIntegerLiteral("123", "0b30", true);
}

static void location1()
{
const char *code;
Expand Down Expand Up @@ -4141,6 +4193,7 @@ static void runTests(int argc, char **argv, Input input)
TEST_CASE(ifalt); // using "and", "or", etc
TEST_CASE(ifexpr);
TEST_CASE(ifUndefFuncStyleMacro);
TEST_CASE(ifIntegerLiteral);

TEST_CASE(location1);
TEST_CASE(location2);
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.