Re: Infinite loop when parsing simple C snippet.

Eric Ludlam <[email protected]> Sun, 14 Feb 2016 09:45:38 -0500
Newsgroups gmane.emacs.semantic
Message-ID <[email protected]>
Hi

I tried your example using emacs 24 w/ the latest from CEDET git and had 
no problems.

I pulled the latest emacs 25 from git (I'm not that familiar with git 
SHA numbers to know if it is a match) and it also worked with no problems.

With both versions however, what should take a small fraction of a 
second took longer.  E24 took 1.1 seconds, and E25 took 2.3 seconds.  If 
I add several more +(A)s, it takes several more seconds, or multiple 
minutes for E25 so it really feels like a hang.

Looking at how expressions are parsed, I can see why it may be a 
problem.  Folks have clearly been adding random bits to it for a while 
where the poor parser is cycling and parsing the same thing over and 
over recursively trying to get through the different options.   This is 
a side effect of the way that parser generator works, and is one of the 
reasons I'd like to upgrade the C/C++ parser to the wisent (bison) 
version.  Maybe someday.

I poked at it to do by hand what bison would do for us and in Emacs 24 
went from 1.1 seconds to .04 seconds on the same code snippet.  The 
speed stays the same even after adding 10 more +(A)s.

The observant on the list will note a strange (- end 1) in the patch. 
I'm not sure why that is yet, but without it the ";" keeps getting stuck 
on the end of the expression even though it isn't being parsed there.

Whatever.

Anyway, this should help a lot.

Eric


On 02/11/2016 08:49 AM, Bastian Beischer wrote:
> Dear all,
>
> I'm trying to debug a problem with the semantic parser which enters an
> infinite loop when parsing the following code:
>
> int A = 1;
> int B = 1;
> int C = 1;
> int D = 1;
> int T = (A)+
>    (B)+
>    (C)+
>    (D);
>
> The problem goes away if the parentheses are removed. It also goes away
> when the "int D = 1" and the corresponding summand below are removed.
>
> This is my output of "M-x cedet-version"
>
> CEDET Version:2.0
> RequestedFileLoaded
>    PackageVersionVersionVersion
>    ----------------------------------------------------------
>    cedet:2.0nilok
>    eieio:1.4nilok
>    semantic:2.2nilok
>    srecode:1.2nilok
>    ede:1.2nil2.0
>
>
> C-h f cedet-version RET
>    for details on output format.
>
>
> You can reproduce the issue as follows.
>
> 1a) Use cedet shipped with emacs from "emacs-25" branch, the exact SHA1
> I used is edc39c92a798821a931f282236f7f4a79c2b25a1
> 1b) I also tried with the same emacs build, but using cedet from cedet
> git's master branch, commit f6639c6ad2ab1a59ffda2b8952d6b31d9ddaf83c
> 2) run emacs -Q
> 3) evaluate (require 'cedet), (require 'semantic)
> 4) open the buffer with the above contents
> 5) run (semantic-force-refresh)
>
> The backtrace in the emacs lisp debugger seems to be this:
>
> Debugger entered--Lisp error: (quit)
>    semantic-c-lexer(67 70 1 nil)
>    semantic-lex(67 70 1 nil)
>    #[(vals start end) "@@@A\306\307\211\310\311
> \f\n\206\312$\"A\242-\207" [vals length depth nonterm end start
> type-cast-list nil semantic-parse-stream semantic-lex 1] 6](((67 . 70))
> 67 70)
>    semantic-parse-stream-default(((INT 45 . 48) (symbol 49 . 50)
> (punctuation 51 . 52) (semantic-list 53 . 56) (punctuation 56 . 57)
> (semantic-list 60 . 63) (punctuation 63 . 64) (semantic-list 67 . 70)
> (punctuation 70 . 71) (semantic-list 74 . 77) (punctuation 77 . 78)) nil)
>    semantic-parse-stream(((INT 45 . 48) (symbol 49 . 50) (punctuation 51
> . 52) (semantic-list 53 . 56) (punctuation 56 . 57) (semantic-list 60 .
> 63) (punctuation 63 . 64) (semantic-list 67 . 70) (punctuation 70 . 71)
> (semantic-list 74 . 77) (punctuation 77 . 78)) nil)
>    semantic-repeat-parse-whole-stream(((INT 1 . 4) (symbol 5 . 6)
> (punctuation 7 . 8) (number 9 . 10) (punctuation 10 . 11) (INT 12 . 15)
> (symbol 16 . 17) (punctuation 18 . 19) (number 20 . 21) (punctuation 21
> . 22) (INT 23 . 26) (symbol 27 . 28) (punctuation 29 . 30) (number 31 .
> 32) (punctuation 32 . 33) (INT 34 . 37) (symbol 38 . 39) (punctuation 40
> . 41) (number 42 . 43) (punctuation 43 . 44) (INT 45 . 48) (symbol 49 .
> 50) (punctuation 51 . 52) (semantic-list 53 . 56) (punctuation 56 . 57)
> (semantic-list 60 . 63) (punctuation 63 . 64) (semantic-list 67 . 70)
> (punctuation 70 . 71) (semantic-list 74 . 77) (punctuation 77 . 78)) nil
> nil)
>    semantic-parse-region-default(1 79 nil nil nil)
>    semantic-parse-region-c-mode(1 79 nil nil nil)
>    semantic-parse-region(1 79)
>    semantic-fetch-tags()
>    semantic-force-refresh()
>
> Which doesn't tell me much without digging into the internals of
> semantic. ​Any help in debugging this ​would be appreciated!
>
> Best regards
> Bastian
>
> --
> Bastian Beischer
> RWTH Aachen University of Technology
>
> @RWTH Aachen
> Office: 28 C 203
> Phone: +49-241-80-27205
> E-mail: [email protected]
> <mailto:[email protected]>
> Address: I. Physikalisches Institut B, Sommerfeldstr. 14, D-52074 Aachen
>
> @CERN
> Office: Bdg 32-4-B12
> Phone: +41-22-76-75750
> E-mail: [email protected] <mailto:[email protected]>
> Address: CERN, CH-1211 Geneve 23
>
>
> ------------------------------------------------------------------------------
> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> Monitor end-to-end web transactions and take corrective actions now
> Troubleshoot faster and improve end-user experience. Signup Now!
> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
>
>
>
> _______________________________________________
> cedet-semantic mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/cedet-semantic
>

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140

_______________________________________________
cedet-semantic mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/cedet-semantic
expression-speed.patch (text/x-patch, 2.5 KB)
diff --git a/lisp/cedet/semantic/bovine/c.by b/lisp/cedet/semantic/bovine/c.by
index 8e0571e..80ed2cf 100644
--- a/lisp/cedet/semantic/bovine/c.by
+++ b/lisp/cedet/semantic/bovine/c.by
@@ -1,6 +1,6 @@
 ;;; semantic/bovine/c.by -- LL grammar for C/C++ language specification
 
-;; Copyright (C) 1999-2012, 2014, 2015 Free Software Foundation, Inc.
+;; Copyright (C) 1999-2012, 2014, 2015, 2016 Free Software Foundation, Inc.
 ;;
 ;; Author: Eric M. Ludlam <[email protected]>
 ;;         David Ponce <[email protected]>
@@ -91,6 +91,7 @@
 %put STATIC summary "Declaration Modifier: static <type> <name> ..."
 %token CONST "const"
 %put CONST summary "Declaration Modifier: const <type> <name> ..."
+%token CONSTEXPR "constexpr"
 %token VOLATILE "volatile"
 %put VOLATILE summary "Declaration Modifier: volatile <type> <name> ..."
 %token REGISTER "register"
@@ -147,6 +148,7 @@
 %token TRY "try"
 %token CATCH "catch"
 %put { TRY CATCH } summary "try { <body> } catch { <catch code> }"
+%token NOEXCEPT "noexcept"
 
 ;; Leave these alone for now.
 %token OPERATOR "operator"
@@ -688,6 +690,7 @@ metadeclmod
 
 CVDECLMOD
   : CONST
+  | CONSTEXPR
   | VOLATILE
   ;
 
@@ -703,6 +706,7 @@ cv-declmods
 METADECLMOD
   : VIRTUAL
   | MUTABLE
+  | EXPLICIT
   ;
 
 ;; C++: A type can be modified into a reference by "&"
@@ -866,6 +870,7 @@ post-fcn-modifiers
   | CONST
   | OVERRIDE
   | FINAL
+  | NOEXCEPT
   ;
 
 opt-throw
@@ -1211,6 +1216,7 @@ expr-start
   | PLUS
   | STAR
   | AMPERSAND
+  | ;; EMPTY
   ;
 
 expr-binop
@@ -1229,12 +1235,14 @@ expr-binop
 ;; Use expression for parsing only.  Don't actually return anything
 ;; for now.  Hopefully we can fix this later.
 expression
-  : unaryexpression QUESTION unaryexpression COLON unaryexpression
-    ( (identity start) (identity end) )
-  | unaryexpression expr-binop unaryexpression
-    ( (identity start) (identity end) )
-  | unaryexpression
-    ( (identity start) (identity end) )
+  : expr-start unaryexpression opt-more-expression
+    ( (identity start) (- end 1) )
+  ;
+
+opt-more-expression
+  : QUESTION unaryexpression COLON unaryexpression
+  | expr-binop unaryexpression
+  | ;; EMPTY
   ;
 
 unaryexpression
@@ -1248,9 +1256,9 @@ unaryexpression
   | string-seq
   | type-cast expression  ;; A cast to some other type
  ;; Casting the results of one expression to something else.
-  | semantic-list expression
+  | semantic-list unaryexpression
   | semantic-list
-  | expr-start expression
+ ;;| expr-start expression
   ;
 
 ;;; semantic/bovine/c.by ends here