Code Monkey home page Code Monkey logo

Comments (11)

rdb avatar rdb commented on June 11, 2024

Thanks! Will apply these.

I wonder if we need to do anything else to handle recursion correctly in all cases - will your fix handle cases where the substitution is slightly different, such as #define sched_priority (sched_priority) ?

from panda3d.

mingodad avatar mingodad commented on June 11, 2024

I've just tested with this:

/* Backward compatibility.  */
#define sched_priority    sched_priority
#define __sched_priority  (sched_priority)

int __sched_priority = 0;

and found that my patch doesn't fix this case.

from panda3d.

rdb avatar rdb commented on June 11, 2024

OK, I'll try to have a go at this - I think every iteration needs to make a blacklist of macros not to expand for the next iteration, or something of the sort.

from panda3d.

mingodad avatar mingodad commented on June 11, 2024

I've just went through it again with gdb and changed the fix (notice it includes fix for the two bugs) and it does work with both cases now:

------------------ dtool/src/cppparser/cppPreprocessor.cxx -------------------
index 878e1c6..443f127 100644
@@ -821,7 +821,7 @@ expand_manifests(const string &input_expr, bool expand_undefined,
           if (mi != _manifests.end()) {
             const CPPManifest *manifest = (*mi).second;
             expand_manifest_inline(expr, q, p, manifest);
-            manifest_found = true;
+            manifest_found = expr != input_expr; //stop if we didn't changed anything
 
           } else if (ident == "__FILE__") {
             // Special case: this is a dynamic definition.
@@ -1455,7 +1455,7 @@ int CPPPreprocessor::
 get_preprocessor_args(int c, string &args) {
   // Following the command, the rest of the line, as well as any text on
   // successive lines, is part of the arguments to the command.
-
+  c = skip_comment(c); //Check for comments first
   while (c != EOF && c != '\n') {
     if (c == '\\') {
       int next_c = get();

from panda3d.

mingodad avatar mingodad commented on June 11, 2024

Another problem that I'm looking now is with <math.h>:

cppparser-nb -v -D__PANDA_CC__ -D__builtin_va_list=void* -Dwchar_t=pccwchar_t -S/usr/include -S/usr/lib/gcc/x86_64-linux-gnu/9/include /usr/include/math.h
Predefining __PANDA_CC__ as 
Predefining __builtin_va_list as void*
Predefining wchar_t as pccwchar_t
Reading /usr/include/math.h
...
  Reading /usr/include/x86_64-linux-gnu/bits/flt-eval-method.h
  Reading /usr/include/x86_64-linux-gnu/bits/fp-logb.h
  Reading /usr/include/x86_64-linux-gnu/bits/fp-fast.h
  Reading /usr/include/x86_64-linux-gnu/bits/mathcalls-helper-functions.h
  Reading /usr/include/x86_64-linux-gnu/bits/mathcalls.h
          /usr/include/x86_64-linux-gnu/bits/mathcalls.h:62:38: error: syntax error, unexpected IDENTIFIER
          __MATHCALL_VEC (cos,, (_Mdouble_ __x));
                                               ^
Error in parsing.

from panda3d.

mingodad avatar mingodad commented on June 11, 2024

Here is the minimum example that shows the problem with <math.h>:

#define __CONCAT(x,y)	x ## y
int __CONCAT(a, __CONCAT(b, c));

gcc output:

gcc -E -P test.c
int a__CONCAT(b, c);

cppparser output:

cppparser-nb -v -D__PANDA_CC__ -D__builtin_va_list=void* -Dwchar_t=pccwchar_t -S/usr/include -S/usr/lib/gcc/x86_64-linux-gnu/9/include test.c
Predefining __PANDA_CC__ as 
Predefining __builtin_va_list as void*
Predefining wchar_t as pccwchar_t
Reading test.c
Finished parsing.
int a__CONCAT;

from panda3d.

rdb avatar rdb commented on June 11, 2024

I think Panda and gcc are parsing those examples similarly; they're both invalid code, Panda just shows the invalid code differently. GCC will show an error if you try to compile it, and parse_file will handle it correctly if you replaced b and c with int b and int c.

As for your other fix, it was still not handling this correctly:

#define A A B C
#define B B C A
#define C C A B

I just checked in a change that handles this correctly by implementing it recursively, with keeping track of already-substituted manifests.

I will close this issue, please open new (individual) issues for other problems you encounter.

Thanks!

from panda3d.

rdb avatar rdb commented on June 11, 2024

A simpler version of what's happening in math.h:

#define __CONCAT(x, y) x ## y

#define __SIMD_DECL(function) __CONCAT(__DECL_SIMD_, function)

#define __MATHCALL_VEC(function, suffix) __SIMD_DECL(__CONCAT(function, suffix))

int __SIMD_DECL(__CONCAT(a, b));

gcc output:

int __DECL_SIMD_ab;

parse_file output:

int __DECL_SIMD___CONCAT;

With CPP_VERBOSE_LEX defined you can see that it's actually being parsed as:

int __DECL_SIMD___CONCAT(a, b);

It should probably be doing expansion on the arguments before passing them into CPPManifest::expand().

from panda3d.

mingodad avatar mingodad commented on June 11, 2024

After all the latest fixes to the preprocessor while trying to parse sqlite3.c I found another macro that result in an endless loop:

#define GLOBAL(t,v) v
#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
int pVfs = vfsList;

gcc output:

gcc -E -P test.c
int pVfs = vfsList;

cppparser output:

cppparser-nb -v -D__PANDA_CC__ -D__builtin_va_list=void* -Dwchar_t=pccwchar_t -S/usr/include -S/usr/lib/gcc/x86_64-linux-gnu/9/include test.c
Predefining __PANDA_CC__ as 
Predefining __builtin_va_list as void*
Predefining wchar_t as pccwchar_t
Reading test.c
  0. START_CPP
  #define GLOBAL(t,v) v
  #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
Pushing to string "vfsList"
  0. START_CONST_EXPR
  End of input stream, restoring to previous input
1. IDENTIFIER vfsList
2. token 0

  1. KW_INT
  2. IDENTIFIER pVfs
  3. '='
  Pushing to expansion " GLOBAL(sqlite3_vfs *, vfsList) "
    Expanding vfsList to  GLOBAL(sqlite3_vfs *, vfsList) 
    Pushing to expansion " GLOBAL(sqlite3_vfs *, vfsList) "
      Expanding GLOBAL to  GLOBAL(sqlite3_vfs *, vfsList) 
      Pushing to expansion " GLOBAL(sqlite3_vfs *, vfsList) "
        Expanding GLOBAL to  GLOBAL(sqlite3_vfs *, vfsList) 
...

from panda3d.

rdb avatar rdb commented on June 11, 2024

Yeah, got it, forgot to make sure it's not expanding manifests it's already in the process of expanding in this situation. Fix incoming.

from panda3d.

rdb avatar rdb commented on June 11, 2024

Fixed in 38a3048, keep it coming!

from panda3d.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.