fursea1 / google-code-prettify

Automatically exported from code.google.com/p/google-code-prettify
Apache License 2.0
0 stars 0 forks source link

C macro Stringizing operator not recognized #157

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When using a C macro with stringized variables, all the code after the # will 
appear greyed-out as a preprocessor directive:

<pre class="lang-c prettyprint-override"><code>#define MIN(a, b) (a &lt; b ? a 
: b)
#define LARGEST_INT_TYPE unsigned long long
#define testimax(TEST_TYPE) {\
  LARGEST_INT_TYPE in, out;\
  size_t pow, limit;\
  TEST_TYPE t;\
  in = 1; out = 2; pow = 0;\
  limit = MIN(sizeof(TEST_TYPE)*8,\
    sizeof(LARGEST_INT_TYPE)*8);\
  while (pow &lt; limit &amp;&amp; out == in + 1) {\
    in = in &lt;&lt; 1;\
    t = (TEST_TYPE) in + 1;\
    out = t;\
    ++pow;\
  }\
  if (pow == limit)\
    puts( #TEST_TYPE " seems integral");\
  else printf (#TEST_TYPE\
    " conversion imprecise for 2^%d+1:\n"\
    "   in: %llu\n  out: %llu\n\n", pow, in + 1, out);\
}

int main(void)
{
    testimax(float);
    testimax(double);
}
</code></pre> 

for `puts( #TEST_TYPE " seems integral");\`, the string after `#TEST_TYPE` 
should be syntax-highlighted as a string. 

Don't know the version: this is from 
http://stackoverflow.com/questions/6060406/using-floats-or-doubles-instead-of-in
ts/6060451#6060451 on Google Chrome 13.0.782.1 dev-m Windows.

Original issue reported on code.google.com by stu...@testtrack4.com on 3 Jun 2011 at 6:34

GoogleCodeExporter commented 9 years ago
How does the C preprocessor lexer distinguish a '#' that starts a preprocessor 
directive from one that does not?

Original comment by mikesamuel@gmail.com on 3 Jun 2011 at 11:42

GoogleCodeExporter commented 9 years ago
Just ran into this as well.

I don't know how it works in C, but in C++ `#` is a preprocessor directive 
exactly when it is either the first nonwhitespace character in the source file 
or is preceded by whitespace containing a newline. 

Reference: C++ working draft N3242, "Preprocessing Directives" 
[http://www.open-std.org/jtc1/sc22/wg21/] 

Original comment by matthew....@gmail.com on 7 Apr 2012 at 11:37

GoogleCodeExporter commented 9 years ago
Related to this issue, I don't know why #include/define/etc are marked as 
comments. I think that C-like languages should have hashComments=false. You can 
fix this issue modifying prettify.js source code. Search for:

  registerLangHandler(sourceDecorator({
          'keywords': CPP_KEYWORDS,
          'hashComments': false,   // This is true in the official release
          'cStyleComments': true,
          'types': C_TYPES
        }), ['c', 'cc', 'cpp', 'cxx', 'cyc', 'm']);

I've attached a patch to fix this issue.

Original comment by davidcapello on 13 Dec 2013 at 7:42

Attachments: