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 — simplecpp misparses '# #' as '##' · Issue #108 · cppcheck-opensource/simplecpp

simplecpp misparses '# #' as '##' #108

New issue
Copy link
New issue
Copy link
Closed
Closed
simplecpp misparses '# #' as '##'#108
Copy link
Assignees
danmar

Description

@hexcoder-
hexcoder-
opened on Oct 3, 2017
Issue body actions

While analyzing issue #60, I found that

#define x # #
x

gives

test.c:1: syntax error: failed to expand 'x', Invalid ## usage when expanding 'x'.

but should give
# #

Activity

  1. danmar commented on Nov 5, 2017

    @danmar
    danmar
    on Nov 5, 2017
    Collaborator
    More actions

    but should give

    hmm.. how do you know that is the expected result. The # is a preprocessor token and should be handled by the preprocessor as far as I know. What use is there in a real program?

  2. hexcoder- commented on Nov 5, 2017

    @hexcoder-
    hexcoder-
    on Nov 5, 2017
    Author
    More actions

    Yes, '#' should be handled by the preprocessor as does '##'. But the semantics between both of them are different.
    -> # and ## operators

    '# #' should not be silently transformed into '##'.

    The 'real use' came from issue #60 while analysing the stack trace.
    For reference cpp of gcc gives this

    >cat demo.cpp
    #define a # #
    a
    
    >cpp -E demo.cpp
    # 1 "<stdin>"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 31 "<command-line>"
    # 1 "/usr/include/stdc-predef.h" 1 3 4
    # 32 "<command-line>" 2
    # 1 "<stdin>"
    
     # #
    

    My intention was like this:
    I created this new issue in order to simplify #60.
    The problem in #60 might be a consequence of misparsing '# #'.
    Since the misparsing happens first, I found it worthwhile to concentrate on this.

  3. amai2012 commented on Nov 7, 2017

    @amai2012
    amai2012
    on Nov 7, 2017
    Contributor
    More actions

    MSVC2012 also gives # #

  4. danmar commented on Nov 11, 2017

    @danmar
    danmar
    on Nov 11, 2017
    Collaborator
    More actions

    ok

  5. asmwarrior commented on Jan 20, 2018

    @asmwarrior
    asmwarrior
    on Jan 20, 2018
    More actions

    I see a very nice header only simple C Preprocessor here:glampert/parse-utils: C++ utilities for lexing and parsing of script languages and configuration files., which is a GPLv3 project, and I just comment out the line:

     preprocessor.hpp | 16 ++++++++--------
     1 file changed, 8 insertions(+), 8 deletions(-)
    
    diff --git a/preprocessor.hpp b/preprocessor.hpp
    index 6973cc3..3d721ba 100644
    --- a/preprocessor.hpp
    +++ b/preprocessor.hpp
    @@ -3022,14 +3022,14 @@ bool preprocessor::expand_macro_and_append(const int macro_index, std::string *
                     const auto & first_tok = m_macro_tokens[macro.first_body_token];
                     const auto & last_tok  = m_macro_tokens[macro.first_body_token + macro.body_token_count - 1];
     
    -                if (lexer::is_punctuation_token(first_tok, lexer::punctuation_id::preprocessor))
    -                {
    -                    return error("\'#\' cannot appear at start of macro expansion!");
    -                }
    -                if (lexer::is_punctuation_token(last_tok, lexer::punctuation_id::preprocessor))
    -                {
    -                    return error("\'#\' cannot appear at end of macro expansion!");
    -                }
    +//                if (lexer::is_punctuation_token(first_tok, lexer::punctuation_id::preprocessor))
    +//                {
    +//                    return error("\'#\' cannot appear at start of macro expansion!");
    +//                }
    +//                if (lexer::is_punctuation_token(last_tok, lexer::punctuation_id::preprocessor))
    +//                {
    +//                    return error("\'#\' cannot appear at end of macro expansion!");
    +//                }
     
                     if (lexer::is_punctuation_token(first_tok, lexer::punctuation_id::preprocessor_merge))
                     {
    
    

    Then I see the parsed result are: # #

    I use this code to run the test: parse-utils/test_eval_macros.cpp at master · glampert/parse-utils

  6. danmar commented on Jan 20, 2018

    @danmar
    danmar
    on Jan 20, 2018
    Collaborator
    More actions

    thanks for pointing out that preprocessor. For information.. a small and simple preprocessor could easily be integrated into cppcheck if it gets good enough and we see that it's actively maintained.

  7. danmar
    self-assigned this
    on Jun 30, 2020
  8. danmar
    added a commit that references this issue on Jun 30, 2020

    Better handling of standalone # in macros (#108)

    546d085
  9. danmar
    closed this as completedon Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @danmar
    danmar

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    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.