How to save the error output of gcc to file - c++

When I compile my code I get a bunch of errors which I span through the screen and I can see where does the error start. How can I save the output of gcc to a file?
I tried tricks like
gcc > log.txt
or grepping the result but it didn't work. Searching google yields mostly result on explaining how to print to file with c++

GCC outputs errors to the standard error stream not to the standard output stream. You need to redirect standard error, instead of standard output. In bash:
gcc 2> log.txt

I personally found out that merely outputing the error to a file would not help. In fact the easiest thing that could help me was to avoid wrapping the error lines which are usually super long. So, I decided to use vim highlighting to see the errors better.
Without the highlighter (View Larger Image)
With the highlighter (View Larger Image)
.
And fortunately, there was a very easy way to set up a new syntax highlighting in VIM.
Follow these steps and you will be more productive working on heavily templated C++ codes:
Create a new VIM custom syntax highlighting rule set
you have to define the syntax highlighting rules. Put the following in a file called cerr.vim and keep it for example in $HOME/vim_syntax/cerr.vim
"Set line wrapping to off to see more error lines in one page
set nowrap
set showmatch
"I use stl and boost alot so it is good to remove the namespaces from the error file :)
silent! %s/st![enter image description here][2]d:://g
silent! %s/boost::fusion:://g
silent! %s/boost:://g
"Usually I am not interested in the file paths until I can locate the error so I tried to
"hide them
silent! %s/\/[^\.]*\// /g
"By default syntax highlighting for each line is limited to 3000 characters
"However, 3000 characters is not sufficient for lengthy C++ errors, so I change it to 20000
set synmaxcol=20000
"Now I define the keywords that I would like them to be highlighted
syn keyword cerrInfo instantiated
syn keyword cerrError error Error ERROR
syn keyword cerrWarning warning Warning WARNING
"-------------------------------------
"In this step I would like to distinguish the prefix in each line (which shows the file name) from the rest of the line
syn region cerrLine start=/^/ end=/\:/
syn region cerrSeparator start=/^\.+/ end=/\./ fold oneline
"I want to make templated type information less visible while debugging
"You have to remember that a type can have nested types. So I define three regions
syn region cerrTemplate1 matchgroup=xBracket1 start=/</ end=/>/ contains=cerrTemplate2 fold oneline
syn region cerrTemplate2 matchgroup=xBracket2 start=/</ end=/>/ contains=cerrTemplate3 fold contained oneline
syn region cerrTemplate3 start=/</ end=/>/ contains=cerrTemplate3 contained oneline fold oneline
"Now I would like to highlight whatever is in parenthesis with a different color so I make
"another region in here. This makes sure that function arguments can have different color
syn region cerrPar matchgroup=xBracket start=/(/ end=/)/ contains=cerrTemplate1 oneline fold
"GCC puts the real type information in brackets, let's group them separately
syn region cerrBracket start=/\[/ end=/\]/ contains=cerrTemplate1,cerrPar oneline
"Again GCC puts the error in these weird characters :) So I define a separate region here
syn region cerrCode start=/‘/ end=/’/ contains=cerrPar,cerrBracket,cerrTemplate1 oneline
"And finally I would like to color the line numbers differently
syn match cerrNum "[0-9]\+[:|,]"
"--------------------------------------------------------------------------
"Now the fun part is here, change the colors to match your terminal colors.
"I Use the following colors for my white background terminal.
"In the following we assign a color for each group that we defined earlier
"Comment is a default VIM color group
highlight link cerrInfo Comment
"We use custom coloring for the rest
highlight default cerrWarning ctermfg=red ctermbg=yellow
highlight default cerrError ctermfg=white ctermbg=red
highlight default cerrLine ctermfg=grey term=bold
highlight default cerrSeparator ctermfg=darkgrey
highlight default cerrTemplate1 ctermfg=grey term=bold
highlight default cerrTemplate2 ctermfg=grey term=bold
highlight default cerrTemplate3 ctermfg=grey
highlight default cerrCode cterm=bold ctermfg=darkgrey
highlight default cerrBracket ctermfg=darkgreen
highlight default xBracket1 ctermfg=darkgrey term=bold
highlight default xBracket2 ctermfg=darkgrey
highlight default cerrPar ctermfg=yellow
highlight default cerrNum ctermfg=red
Change your .vimrc file
Now, you have to tell vim to use your new highlighting for files with specific extension. In my case I would like to output my error files to error.ccerr, put the following in your .vimrc in your home folder:
au BufRead,BufNewFile *.cerr set filetype=myerror
au Syntax myerror source $HOME/vim_syntax/cerr.vim
What I say in the above is that when files with the extension .cerr are opened using VIM, they will be considered of type myerror. In the second line I am saying that VIM should use my syntax highlighting rule set which I defined in the previous step for all myerror files.
Send your error output to a .cerr file and open it with VIM
This step is the easiest, we send all errors and warning to error.cerr and if there is any error in the file we immediately open the .cerr file using VIM.
g++ failing.cc &> error.cerr || vim error.cerr

Related

Vim syntax highlight two specific words with a space (regex)

I have created this file called "~/.vim/syntax/proc.vim" and have been populating regex expressions (I think that's what they call it). I'm having to write some test scripts in this test language developed back in the 90's called STOL for a spacecraft payload. I'm working in a secured environment so the only thing I have access to is vim.
STOL lets you write several different types of print statements and I would like some syntax highlighting difference between each type of message (error message, info message, etc).
My vim color profile called "~/.vim/color/molokai.vim" lets me link various regex expressions to a specific syntax class which will invoke a specific color. For example, to highlight keywords I specify two lines like...
" This is the regex where two keywords have been defined
syntax keyword procKeywords IF ELSE
" This is how I link the above regex to a molokai color class
" which is called Keyword
highlight link procKeywords Keyword
I would essentially like to do the same for some error and info messages that STOL defines like the following...
EVENT ERROR "This is a error message"
EVENT INFO "This is a info message"
How can I match on two specific words with a space between them? I need something like the following in my "~/.vim/syntax/proc.vim". The following is wrong but I'm just writing it out showing you what I'm thinking...
syntax match procInfo "EVENT INFO"
syntax match procError "EVENT ERROR"
highlight link procInfo ModeMsg
highlight link procError ErrorMsg
Your attempt already mostly works. However, :syntax match does not automatically enforce whole-keyword match like :syntax keyword, so you'd get false highlighting on e.g. MYEVENT INFORMATION, too. You can easily fix that by adding the keyword boundary atoms :help /\< to the regular expressions:
syntax match procInfo "\<EVENT INFO\>"
syntax match procError "\<EVENT ERROR\>"

Sublime C++ namespace keyword color scheming

I am having an annoying behavior in Sublime.
When I start typing out the line...
using namespace SomeNamespace;
The keywords 'using' and 'namespace' are properly colored the keyword coloring. Then when I add the semicolon to the end of the line, the namespace keyword goes white (default text color). I know this is not that significant, but it really annoys me.
Has anyone noticed this behavior before? The code compiles without errors or warnings, so I know sublime is not detecting some so of code problem.
Does anyone have any suggestions on how to fix this problem?
The problem is in this particular regex in the C++ syntax definition:
\b(namespace)\s+([A-Za-z_][_A-Za-z0-9:]*\b)?+(?!\s*?(;|=|,))
At the very end, in the negative lookahead - (?!...) - we see that semicolons are excluded from the match, meaning that if a semicolon is present at the very end of the line, there's no match.
To fix it, you'll need to install the very useful PackageResourceViewer plugin from Package Control. Then, open the Command Palette, type prv to bring up the PackageResourceViewer options, select the Extract Package one, then scroll down and select C++. There will now be a C++ directory in the directory opened by choosing Preferences -> Browse Packages.... Go into that directory and you'll see a bunch of files. Depending on what version of Sublime Text 3 you're using, you'll want to open either C++.tmLanguage or C++.sublime-syntax in Sublime. The .tmLanguage format is XML, so you can pick that for syntax highlighting if you wish, while the .sublime-syntax file is in YAML.
Once the appropriate file is open (you'll either have one or the other, not both), search for the regex above, or just search for namespace, you should find it pretty easily. Delete the ;| from near the end, making the whole thing:
\b(namespace)\s+([A-Za-z_][_A-Za-z0-9:]*\b)?+(?!\s*?(=|,))
Save the file, and that's it! Your C++ source files should update their behavior immediately - if not, just close and reopen them, and in the worst case you can just close them, restart Sublime, then reopen them.

vim syntax folding (unintentionally) creates nested folds

I'm trying to implement vim folding into an existing syntax file for the fountain.io markup language. The existing syntax file is here: http://www.vim.org/scripts/script.php?script_id=3880
But no matter what I do, my folding region doesn't end where I expect it to. The regexes work perfectly when I test them in search. But when used in a syntax region they created a series of nested folds. The fold starts on the appropriate line, and then the next fold is created inside the existing fold. Essentially I've tried to do the following so that a fold begins on any line that starts with INT or EXT, and ends after a line ends in TO: or a line begins with >:
syn region fountainScene start="^\(INT\|EXT\)" end="^\(\(\L\)* TO:\|\s*>\(.*\)\)$" fold transparent contains=fountainCharacter,fountainDialogue,fountainParenthetical,fountainSceneHeading,fountainTransition
Even stranger, if I use \ze on my 'end' argument to get the line previous to the matched line, it works as you would expect. It stops the fold on the line above the match, leaving the last line outside the fold. The following will stop folding above a line that says "CUT TO:"
\n\ze\(\L\)* TO:\n
To troubleshoot, I basically started rebuilding the syntax file from the ground up. Here is what is in the file so far (minus the hi commands) I have extensively modified the regular expressions to prevent them from overlapping with one another. The original regexes had this issue and I thought it might be the cause:
syn match fountainCharacter "^\(\s\)*\n\zs\(INT\|EXT\)\#!\(\L\)*[^:]$"
syn region fountainDialogue matchgroup=fountainCharacter start="^\(\s\)*\n\zs\(INT\|EXT\)\#!\(\L\)*[^:]$" end="^\s*$" contains=fountainCharacter,fountainParenthetical
syn match fountainParenthetical "^\s*\((.*)\)$"
syn region fountainSceneHeading start="^\(INT\|EXT\)" end="$" contains=fountainSceneNumber,fountainBoneyard,fountainNotes
syn match fountainTransition "^\(\L\)* TO:$"
syn region fountainScene start="^\(INT\|EXT\)" end="\n\ze\(\L\)* TO:\n" fold transparent contains=fountainCharacter,fountainDialogue,fountainParenthetical,fountainSceneHeading
Thanks for any help you can provide, and please let me know if I've been unclear in any way. I am using MacVim version 7.3.646 custom compiled with python support.
I finally figured this out. One of my elements was extending beyond the "end" argument. I had to use the "keepend" argument in my syn region.
This line fixed everything:
syn region fountainScene start="^\s*\(\.\|INT\. \|EXT\. \|INT\./EXT\. \|INT/EXT\. \|INT \|EXT \|INT/EXT \|I/E \|int\. \|ext\. \|int\./ext\. \|int/ext\. \|int \|ext \|int/ext \|i/e \)" end="^\(\(\L\)* TO:\|\s*>[^<]*\)$" fold transparent keepend
For more details see :he keepend

syntax highlighting between matching parenthesis

Say I hava a LaTeX document open in Vim, and I want to highlight every occurence of
{\color{red} ... }
(where the dots are supposed to symbolize some contents), that is, I want to have {\color{red}, } and everything between these highlighted. This I have done with
:syn region WarningMsg start=+{\\color{red}+ end=+}+
but I have the problem that, if I write something like {\color{red} some{thing} important}, then it is only {\color{red} some{thing} which gets highlighted, because Vim of course mathces the first occurrence of }. How can I make this Highlighting rule so that it skips matching curly brackets? Even multiple levels of such.
For clarity it is better to give each syntax region a bespoke name, and then link it to a standard colour group. I have renamed your original region redTeX.
You need to define a second region, innerBrace, defining the braces you want to ignore, and mark this region as transparent. Then redTeX should be marked to contain the transparent region, which it will then ignore.
syn region innerBrace start=+{+ end=+}+ transparent contains=redTeX
syn region redTeX start=+{\\color{red}+ end=+}+ contains=innerBrace
hi link redTeX WarningMsg
Note that in this case there is the added subtlety that redTeX itself matches as an innerBrace. I fixed this by marking innerBrace as containing redTeX.
Hope that makes sense!

class & function names highlighting in Vim

I just recently set up my Vim environment from Textmate, after becoming addicted to its modal input.
However, syntax highlighting seems to be not so beautiful in Vim. I code in C++ and since the function call and class names can't be highlighted, the code is more difficult to read. I played with color scheme for a bit, but couldn't find any field that corresponded to "class name" or "function name".
In the picture below, notice how DroughtLayer:: and *.size() is not highlighted on the right in MacVim.
(source: ivzhao.com)
Any ideas how to solve this? It really annoys me as I am so much a visual-sensitive guy.
I had this very same problem when I started using vim. The solution is simple, you just have to edit the c syntax file used by vim, here's how to do it:
When you start editing a C or C++ file, vim reads the default c syntax file located in
$VIMRUNTIME/syntax/c.vim
(Where $VIMRUNTIME is where you have vim installed. You can find out it's default value by opening vim and using the command ":echo $VIMRUNTIME").
You can simply overwrite that file, or you can create your custom C syntax file (which will be loaded by vim instead of the default one) in this location:
$HOME/.vim/syntax/c.vim (for UNIX)
$HOME/vimfiles/syntax/c.vim (for PC or OS/2)
(I have never used a Mac so I don't know which one will work for you. You can find out more in the vim help, ":help vimfiles")
Now the fun part. Copy the default "$VIMRUNTIME/syntax/c.vim" file to your vimfiles directory ("$HOME/.vim/syntax/c.vim" for UNIX), and edit it by adding these lines:
" Highlight Class and Function names
syn match cCustomParen "(" contains=cParen,cCppParen
syn match cCustomFunc "\w\+\s*(" contains=cCustomParen
syn match cCustomScope "::"
syn match cCustomClass "\w\+\s*::" contains=cCustomScope
hi def link cCustomFunc Function
hi def link cCustomClass Function
That's it! Now functions and class names will be highlighted with the color defined in the "Function" highlight (":hi Function"). If you want to customize colors, you can change the last two lines above to something like this:
hi def cCustomFunc gui=bold guifg=yellowgreen
hi def cCustomClass gui=reverse guifg=#00FF00
or you can leave the C syntax file alone and define colors in your vimrc file (":help vimrc"):
hi cCustomFunc gui=bold guifg=yellowgreen
hi cCustomClass gui=reverse guifg=#00FF00
(Note the absence of the "def" keyword, go to ":help highlight-default" for details). For the available parameters to the ":hi" command see ":help :highlight".
You can find the complete c.vim file for Vim 7.2 on this link (Note: only use this if you have a non-modified Vim, version 7.2):
http://pastebin.com/f33aeab77
And the obligatory screenshot:
this is my first post here and i didn't know how to make an observation, the answer of Eduardo makes "(" and "{" look unmached and bugs syntax foldind, I changed it a little to fix this.
syn match cCustomParen "?=(" contains=cParen,cCppParen
syn match cCustomFunc "\w\+\s*(\#=" contains=cCustomParen
syn match cCustomScope "::"
syn match cCustomClass "\w\+\s*::" contains=cCustomScope
hi def cCustomFunc gui=bold guifg=yellowgreen
hi def link cCustomClass Function
Interestingly, the syntax highlighters in VIM don't support applying a syntax to identifiers or function names - at least not the syntax highlighters for C and C++. So, even if you do:
:hi Function guifg=red
or
:hi Identifier guifg=red
it doesn't give these a color. I just seems to be not much more than keywords and constants for these languages.
Here, someone has started extending the cpp syntax file to support method names. It's a start I guess.
http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition
The one solution is to use built ctags database. So create one with the ctags utility. Then set the 'tags' variable and put the following to the
~/.vim/after/syntax/c.vim
function! s:highlight()
let list = taglist('.*')
for item in list
let kind = item.kind
if kind == 'f' || kind == 'c'
let name = item.name
exec 'syntax keyword Identifier '.name
endif
endfor
endfunction
call s:highlight()
I must warn you that this can work very slow on the very big ctags database.
Also there is one solution on the vim.org but I didn't try this one. Let me know if it works for you.
EDIT: color_coded may be too heavy for you. try octol/vim-cpp-enhanced-highlight. It supports C++11/14 and integrates what #Eduardo answers.
Semantic based highlighter:
I would recommend jeaye/color_coded,
A vim plugin for libclang-based highlighting
So sorry that i'm new to stackoverflow which means I've not enough reputation to post images. Go see its effects if you wanna give it a shot. :)
Pros:
Easy installation
Semantic highlighting
Clighter mentioned as above, need vim compiled with python2.7.
However, color_coded is written in C++ and provides lua binding ->
C++.
Cons:
It delays unless you make some vim events to acitve it.
Customization is bit harder; you need to edit syntax/color_coded.vim
yourself. But customization has been placed on its roadmap.
Although it's still under development, it's increasingly gaining attention.
Sergey, changing the first line from
syn match cCustomParen "(" contains=cParen,cCppParen
to
syn match cCustomParen "(" contains=cParen contains=cCppParen
seems to fix it for me.
Try using this plugin http://www.vim.org/scripts/script.php?script_id=2646
Its does all ctags highlighting very efficiently for you
Use a plug-in for vim like Taglist or set up ctags or cscope integration with vim (here's a tutorial for the vim/cscope.)
I really recommend you the taghighlight plugin, click here for it's website.
The Clighter plugin can also be considered, which is a
plugin for c-family semantic source code highlighting, based on Clang
However, requires fairly recent versions and software: vim 7.4.330 +python2 and libclang.
To match C functions definitions only, this works for me:
syn match cCustomFuncDef display /\(\w\+\(\s\|*\)\+\)\#<=\w\+\s*(\#=/
hi def cCustomFuncDef ctermfg=lightblue