Skip to content

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

Closed

Description

@hexcoder-

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
    Collaborator

    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-
    Author

    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
    Contributor

    MSVC2012 also gives # #

  4. danmar commented on Nov 11, 2017

    @danmar
    Collaborator

    ok

  5. asmwarrior commented on Jan 20, 2018

    @asmwarrior

    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
    Collaborator

    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. self-assigned this
    on Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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