Wrongly highlited comments in #include lines

Issue #1458 new
Ioannis Papadopoulos
created an issue

The comments in a "#include ..." line are not highlighted correctly, as can be seen in the image below.

img1.png

Sample C++ code to show the problem:

#include <iostream>
#include <stdlib.h>     // for the random numbers in main...
#include     <sys/types.h>  // for getpid()
#include /* inline comment 1 */ <unistd.h>     /* in_c_1   */   // for getpid()
#include                        <unistd.h>     /* in_c_2   */
#include /* inline com"ment 2 */ <unistd.h>                      // for getpid()
#include <stdio.h>     // for sp<r"intf()

// Next include lines for pygments test only...
#include "stdio.h"     // for spr"intf() /* not_inline  < > <" "" >>>
#include "<stdio.h">     // for spr"intf() /* not_inline  < > <" "" >>>
#include <"stdio.h>"     // for spr"intf() /* not_inline  < > <" "" >>>
#include stdio.h     // for spr"intf() /* not_inline  < > <" "" >>>
#include /* abc */ stdio.h     // for spr"intf() /* not_inline  < > <" "" >>>

using namespace std;    // an inline comment...

/*! \file matrix.cpp This is a class to manipulate 2D matrices.
    It also contain a main program to test its functionality.
 */

//! This is a class to manipulate 2D matrices.

/*! The Matrix objects are constructed providing their dimensions.<br>
    The matrix elements should be set via the #Mij method.<br>
    Various matrix operations are defined for the Matrix class.
 */

//------------------------------------------------------
class Matrix {
public:
  //! Constructor
  Matrix(int m, int n);

Comments (2)

  1. Ioannis Papadopoulos reporter

    I have a modification for c_cpp.py that solves this issue:

    # HG changeset patch
    # User pyannis
    # Date 1531996802 -7200
    #      Thu Jul 19 12:40:02 2018 +0200
    # Node ID d304b1e694c10efc86d674b3f0472073de92f0b6
    # Parent  7941677dc77d4f2bf0bbd6140ade85a9454b8b80
    Addresses Issue #1458
    
    diff -r 7941677dc77d -r d304b1e694c1 pygments/lexers/c_cpp.py
    --- a/pygments/lexers/c_cpp.py  Mon Mar 13 19:16:03 2017 +0000
    +++ b/pygments/lexers/c_cpp.py  Thu Jul 19 12:40:02 2018 +0200
    @@ -127,8 +127,8 @@
                 (r'\\', String),  # stray backslash
             ],
             'macro': [
    -            (r'(include)(' + _ws1 + r')([^\n]+)',
    -             bygroups(Comment.Preproc, Text, Comment.PreprocFile)),
    +            (r'(include)('+_ws1+r')("[^"]+")([^\n]*)', bygroups(Comment.Preproc, using(this), Comment.PreprocFile, Comment.Single)),
    +            (r'(include)('+_ws1+r')(<[^>]+>)([^\n]*)', bygroups(Comment.Preproc, using(this), Comment.PreprocFile, Comment.Single)),
                 (r'[^/\n]+', Comment.Preproc),
                 (r'/[*](.|\n)*?[*]/', Comment.Multiline),
                 (r'//.*?\n', Comment.Single, '#pop'),
    

    Using this modified c_cpp.py, with the same C++ code as in the previous comment, I get what I expect: img2.png

  2. Log in to comment