Finding unbalanced braces and parentheses
gcc 4.6.0 GNU Emacs 23.2.1
I have some c code and at some point I must have made a typing mistake. And now I am left with unbalanced curly braches or a parentheses.
I have about 2000 lines of code and I am just wondering is there any technique for finding them?
Emacs has some good features, so I am just wondering is there any way it can scan the code and tell me where they开发者_C百科 are. Currently I have a compile error related to this.
Many thanks for any suggestions,
In emacs you can use M-x check-parens
. See http://www.gnu.org/software/libtool/manual/emacs/Parentheses.html
I would suggest adding "#ifdef 0 .... #endif" around blocks of code until the source compiles. Keep adjusting the size of the ifdef'd out code until you narrow down a chunk that compiles when ifdef'd out and doesn't otherwise. This chunk will contain the unbalanced brace.
Personally I'd start by ifdefing out almost the whole file and moving my "#ifdef 0" ahead each function until I started seeing compilation errors.
If you have crazy long functions then take the same sort of approach but within the function that's giving you trouble (once you've determined which function it is).
One good point to start is selecting all (M-x select-all
) and then indenting the code (M-x indent-region
). As emacs does a very good job indenting, you'll notice where the indentation failed.
Also, using show-paren-mode
(M-x show-paren-mode
) emacs will show you the matching paren of each ending paren (and vice-versa), so you can go through your code.
If you want something more fancy than plain highlighting, you could use rainbow delimiters: https://www.emacswiki.org/emacs/RainbowDelimiters
Here is a short blog post on it: http://emacs-fu.blogspot.com/2011/05/toward-balanced-and-colorful-delimiters.html
精彩评论