Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong C++ highlighting #41

Open
mattn opened this issue Aug 28, 2016 · 14 comments
Open

Wrong C++ highlighting #41

mattn opened this issue Aug 28, 2016 · 14 comments

Comments

@mattn
Copy link
Member

mattn commented Aug 28, 2016

vim/vim#1005

@mattn
Copy link
Member Author

mattn commented Aug 29, 2016

I'm not sure, but removing ^ fixes this syntax.

diff --git a/syntax/c.vim b/syntax/c.vim
index 85042e4..b7dedb3 100644
--- a/syntax/c.vim
+++ b/syntax/c.vim
@@ -388,11 +388,11 @@ if !exists("c_no_if0")
 endif
 syn region cIncluded   display contained start=+"+ skip=+\\\\\|\\"+ end=+"+
 syn match  cIncluded   display contained "<[^>]*>"
-syn match  cInclude    display "^\s*\(%:\|#\)\s*include\>\s*["<]" contains=cIncluded
+syn match  cInclude    display "\s*\(%:\|#\)\s*include\>\s*["<]" contains=cIncluded
 "syn match cLineSkip   "\\$"
 syn cluster    cPreProcGroup   contains=cPreCondit,cIncluded,cInclude,cDefine,cErrInParen,cErrInBracket,cUserLabel,cSpecial,cOctalZero,cCppOutWrapper,cCppInWrapper,@cCppOutInGroup,cFormat,cNumber,cFloat,cOctal,cOctalError,cNumbersCom,cString,cCommentSkip,cCommentString,cComment2String,@cCommentGroup,cCommentStartError,cParen,cBracket,cMulti,cBadBlock
-syn region cDefine     start="^\s*\(%:\|#\)\s*\(define\|undef\)\>" skip="\\$" end="$" keepend contains=ALLBUT,@cPreProcGroup,@Spell
-syn region cPreProc    start="^\s*\(%:\|#\)\s*\(pragma\>\|line\>\|warning\>\|warn\>\|error\>\)" skip="\\$" end="$" keepend contains=ALLBUT,@cPreProcGroup,@Spell
+syn region cDefine     start="\s*\(%:\|#\)\s*\(define\|undef\)\>" skip="\\$" end="$" keepend contains=ALLBUT,@cPreProcGroup,@Spell
+syn region cPreProc    start="\s*\(%:\|#\)\s*\(pragma\>\|line\>\|warning\>\|warn\>\|error\>\)" skip="\\$" end="$" keepend contains=ALLBUT,@cPreProcGroup,@Spell

 " Highlight User Labels
 syn cluster    cMultiGroup contains=cIncluded,cSpecial,cCommentSkip,cCommentString,cComment2String,@cCommentGroup,cCommentStartError,cUserCont,cUserLabel,cBitField,cOctalZero,cCppOutWrapper,cCppInWrapper,@cCppOutInGroup,cFormat,cNumber,cFloat,cOctal,cOctalError,cNumbersCom,cCppParen,cCppBracket,cCppString

@rhysd
Copy link
Contributor

rhysd commented Sep 1, 2016

I think \s* at the head of pattern should be removed as well.

@mattn
Copy link
Member Author

mattn commented Sep 1, 2016

right

@brammool
Copy link

Since when is putting a comment before #if valid C?

@mattn
Copy link
Member Author

mattn commented Nov 18, 2016

@brammool This should fix it.

8976dec

diff --git a/syntax/c.vim b/syntax/c.vim
index cc99f67..82148e7 100644
--- a/syntax/c.vim
+++ b/syntax/c.vim
@@ -358,8 +358,8 @@ if !exists("c_no_c99") " ISO C99
 endif

 " Accept %: for # (C99)
-syn region cPreCondit  start="^\s*\zs\(%:\|#\)\s*\(if\|ifdef\|ifndef\|elif\)\>" skip="\\$" end="$" keepend contains=cComment,cCommentL,cCppString,cCharacter,cCppParen,cParenError,cNumbers,cCommentError,cSpaceError
-syn match  cPreConditMatch display "^\s*\zs\(%:\|#\)\s*\(else\|endif\)\>"
+syn region cPreCondit  start="\s*\zs\(%:\|#\)\s*\(if\|ifdef\|ifndef\|elif\)\>" skip="\\$" end="$" keepend contains=cComment,cCommentL,cCppString,cCharacter,cCppParen,cParenError,cNumbers,cCommentError,cSpaceError
+syn match  cPreConditMatch display "\s*\zs\(%:\|#\)\s*\(else\|endif\)\>"
 if !exists("c_no_if0")
   syn cluster  cCppOutInGroup  contains=cCppInIf,cCppInElse,cCppInElse2,cCppOutIf,cCppOutIf2,cCppOutElse,cCppInSkip,cCppOutSkip
   syn region   cCppOutWrapper  start="^\s*\zs\(%:\|#\)\s*if\s\+0\+\s*\($\|//\|/\*\|&\)" end=".\@=\|$" contains=cCppOutIf,cCppOutElse,@NoSpell fold

@mattn
Copy link
Member Author

mattn commented Nov 18, 2016

@brammool or, do you ask me why need this change?

@mattn
Copy link
Member Author

mattn commented Nov 18, 2016

@brammool Ah, I understanded it just now. vim/vim#1257 (comment)

@mattn
Copy link
Member Author

mattn commented Apr 26, 2017

I rebased this repository. And reverted 8976dec

So this issue still are remaining.

@ghost
Copy link

ghost commented May 14, 2017

@brammool: This was likely always valid C/C++, you can check yourself with gcc or g++. The requirement seems to be the line needs to be devoid of statements before the preprocessor directive.

(void)0; #ifdef POSIX
#endif
error: stray # in program
(void)0; #ifdef POSIX
         ^

and an ensuing error about the mismatched #endif.

@brammool
Copy link

brammool commented May 15, 2017 via email

@ghost
Copy link

ghost commented May 15, 2017

@brammool: That was a counterexample, the bug was about this case:

/**/ #ifdef POSIX
#endif

Which isn't highlighted properly by Vim (or GitHub), despite being valid C/C++. There may be any amount of whitespace before a preprocessor directive on a line and comments seem to count as part of this whitespace. This will compile whereas the other example does not.

@brammool
Copy link

brammool commented May 16, 2017 via email

@ghost
Copy link

ghost commented May 17, 2017

@brammool: Okay, but C89 comments are properly detected already. It seems like the fix involves relaxing the requirement that the # start the line, and the directive text follow immediately after it.

This one is more of a syntactic oddity but there are other issues open (#43, #49) about syntax highlighting deficiencies that are related to this one. I don't understand Vim enough to fix them, but I can vouch that most of what has been reported are actual things found in many projects.

But that's kind of besides the point - the standard allows such syntax, and the highlighter should, ideally, support it. I understand there will be limitations but this isn't yet trying to parse and understand C/C++.

@mattn
Copy link
Member Author

mattn commented May 17, 2017

To fix this, it need the regexp pattern enough to be confusable. And it will take heavy load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@mattn @rhysd @brammool and others