Skip to content

Commit 3a8a81e

Browse files
committed
Store LOC if Xref is used while analyzing
1 parent 7c9872f commit 3a8a81e

File tree

94 files changed

+1964
-1038
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1964
-1038
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
*/
23+
24+
%{
25+
/**
26+
* Calls {@link #onSourceCodeSeen(int)} with {@code yychar}.
27+
*/
28+
public void phLOC() { onSourceCodeSeen(yychar); }
29+
%}

src/org/opensolaris/opengrok/analysis/FileAnalyzer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,15 @@ protected void addNumLines(Document doc, int value) {
256256
doc.add(new StoredField(QueryBuilder.NUML, value));
257257
}
258258

259+
/**
260+
* Add a field to store document lines-of-code.
261+
* @param doc the target document
262+
* @param value the loc
263+
*/
264+
protected void addLOC(Document doc, int value) {
265+
doc.add(new StoredField(QueryBuilder.LOC, value));
266+
}
267+
259268
private JFlexTokenizer createPlainSymbolTokenizer() {
260269
return new JFlexTokenizer(new PlainSymbolTokenizer(
261270
FileAnalyzer.dummyReader));

src/org/opensolaris/opengrok/analysis/JFlexJointLexer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ boolean offerSymbol(String value, int captureOffset, boolean ignoreKwd)
7979
* @throws IOException if an output error occurs
8080
*/
8181
void disjointSpan(String className) throws IOException;
82+
83+
/**
84+
* Indicates that eligible source code was encountered for physical
85+
* lines-of-code count (physical LOC).
86+
*/
87+
void phLOC();
8288
}

src/org/opensolaris/opengrok/analysis/JFlexNonXref.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ public abstract class JFlexNonXref extends JFlexStateStacker
7575
protected Scope scope;
7676
private int scopeLevel;
7777

78+
/**
79+
* The following field is set to {@code true} (via {@link #phLOC()}) when
80+
* applicable during lexing before a call to {@link #startNewLine()} so
81+
* that the lines-of-code count is also incremented.
82+
*/
83+
protected boolean didSeePhysicalLOC;
84+
85+
protected int loc;
86+
7887
/**
7988
* See {@link RuntimeEnvironment#getUserPage()}. Per default initialized in
8089
* the constructor and here to be consistent and avoid lot of unnecessary
@@ -137,13 +146,23 @@ public void reset() {
137146
super.reset();
138147

139148
annotation = null;
149+
didSeePhysicalLOC = false;
140150
disjointSpanClassName = null;
151+
loc = 0;
141152
scopes = new Scopes();
142153
scope = null;
143154
scopeLevel = 0;
144155
scopeOpen = false;
156+
setLineNumber(1);
145157
}
146158

159+
/**
160+
* Gets the document physical lines-of-code count.
161+
* @return a number greater than or equal to 0
162+
*/
163+
@Override
164+
public int getLOC() { return loc; }
165+
147166
@Override
148167
public void setAnnotation(Annotation annotation) {
149168
this.annotation = annotation;
@@ -178,6 +197,11 @@ public void setFoldingEnabled(boolean foldingEnabled) {
178197
this.foldingEnabled = foldingEnabled;
179198
}
180199

200+
/**
201+
* Sets a value indicating that a physical line-of-code was encountered.
202+
*/
203+
protected void phLOC() { didSeePhysicalLOC = true; }
204+
181205
/**
182206
* Calls {@link #appendLink(java.lang.String, boolean)} with {@code url}
183207
* and false.
@@ -347,6 +371,11 @@ public void startNewLine() throws IOException {
347371
boolean skipNl = false;
348372
setLineNumber(line + 1);
349373

374+
if (didSeePhysicalLOC) {
375+
++loc;
376+
didSeePhysicalLOC = false;
377+
}
378+
350379
if (scopesEnabled) {
351380
startScope();
352381

src/org/opensolaris/opengrok/analysis/JFlexSymbolMatcher.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ protected void onSymbolMatched(String str, int start) {
103103
}
104104
}
105105

106+
/**
107+
* Raises
108+
* {@link SymbolMatchedListener#sourceCodeSeen(org.opensolaris.opengrok.analysis.SourceCodeSeenEvent)}
109+
* for all subscribed listeners in turn.
110+
* @param start the source code start position
111+
*/
112+
protected void onSourceCodeSeen(int start) {
113+
SymbolMatchedListener l = symbolListener;
114+
if (l != null) {
115+
SourceCodeSeenEvent evt = new SourceCodeSeenEvent(this, start);
116+
l.sourceCodeSeen(evt);
117+
}
118+
}
119+
106120
/**
107121
* Calls {@link #onNonSymbolMatched(java.lang.String, int)} with the
108122
* {@link String#valueOf(char)} {@code c} and {@code start}.

src/org/opensolaris/opengrok/analysis/JFlexTokenizer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ public void symbolMatched(SymbolMatchedEvent evt) {
108108
setAttribs(evt.getStr(), evt.getStart(), evt.getEnd());
109109
}
110110

111+
/**
112+
* Does nothing.
113+
* @param evt ignored
114+
*/
115+
@Override
116+
public void sourceCodeSeen(SourceCodeSeenEvent evt) {
117+
}
118+
111119
/**
112120
* Clears, and then resets the instance's attributes per the specified
113121
* arguments.

src/org/opensolaris/opengrok/analysis/JFlexXref.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public class JFlexXref implements Xrefer, SymbolMatchedListener,
6060
private Scope scope;
6161
private int scopeLevel;
6262

63+
/**
64+
* The following field is set to {@code true} (via
65+
* {@link #sourceCodeSeen(org.opensolaris.opengrok.analysis.SourceCodeSeenEvent)})
66+
* when applicable during lexing before a call to {@link #startNewLine()}
67+
* so that the lines-of-code count is also incremented.
68+
*/
69+
private boolean didSeePhysicalLOC;
70+
71+
private int loc;
72+
6373
/**
6474
* See {@link RuntimeEnvironment#getUserPage()}. Per default initialized in
6575
* the constructor and here to be consistent and avoid lot of unnecessary
@@ -124,7 +134,9 @@ public void setReader(Reader input) {
124134
@Override
125135
public void reset() {
126136
annotation = null;
137+
didSeePhysicalLOC = false;
127138
disjointSpanClassName = null;
139+
loc = 0;
128140
scopes = new Scopes();
129141
scope = null;
130142
scopeLevel = 0;
@@ -136,10 +148,20 @@ public void reset() {
136148
* of the instance's {@link ScanningSymbolMatcher}.
137149
* @return yyline
138150
*/
151+
@Override
139152
public int getLineNumber() {
140153
return matcher.getLineNumber();
141154
}
142155

156+
/**
157+
* Gets the determined count of physical lines-of-code.
158+
* @return lines-of-code count
159+
*/
160+
@Override
161+
public int getLOC() {
162+
return loc;
163+
}
164+
143165
@Override
144166
public void setAnnotation(Annotation annotation) {
145167
this.annotation = annotation;
@@ -187,6 +209,17 @@ public void symbolMatched(SymbolMatchedEvent evt) {
187209
}
188210
}
189211

212+
/**
213+
* Sets a value indicating that source code was encountered such that
214+
* {@link #getLOC()} will be incremented upon the next
215+
* {@link #startNewLine()} or at end-of-file.
216+
* @param evt ignored
217+
*/
218+
@Override
219+
public void sourceCodeSeen(SourceCodeSeenEvent evt) {
220+
didSeePhysicalLOC = true;
221+
}
222+
190223
@Override
191224
public void nonSymbolMatched(TextMatchedEvent evt) {
192225
String str = evt.getStr();
@@ -463,19 +496,33 @@ public void write(Writer out) throws IOException {
463496
while (!matcher.emptyStack()) {
464497
matcher.yypop();
465498
}
499+
500+
// Account for dangling line of code. Unlike line number, LOC is
501+
// incremented post- rather than pre-.
502+
if (didSeePhysicalLOC) {
503+
++loc;
504+
didSeePhysicalLOC = false;
505+
}
466506
}
467507

468508
/**
469509
* Terminate the current line and insert preamble for the next line. The
470-
* line count will be incremented.
471-
*
510+
* line count is taken from {@link ScanningSymbolMatcher#getLineNumber()}.
511+
* The lines-of-code count may be incremented if {@link #didSeePhysicalLOC}
512+
* has been set to {@code true} (after which it will be reset to
513+
* {@code false}).
472514
* @throws IOException on error when writing the xref
473515
*/
474516
public void startNewLine() throws IOException {
475517
String iconId = null;
476518
int line = matcher.getLineNumber();
477519
boolean skipNl = false;
478520

521+
if (didSeePhysicalLOC) {
522+
++loc;
523+
didSeePhysicalLOC = false;
524+
}
525+
479526
if (scopesEnabled) {
480527
startScope();
481528

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package org.opensolaris.opengrok.analysis;
25+
26+
/**
27+
* Represents an event raised when a language lexer has seen source code.
28+
*/
29+
public class SourceCodeSeenEvent {
30+
31+
private final Object source;
32+
private final int position;
33+
34+
/**
35+
* Initializes an immutable instance of {@link SourceCodeSeenEvent}.
36+
* @param source the event source
37+
* @param position the text position
38+
*/
39+
public SourceCodeSeenEvent(Object source, int position) {
40+
this.source = source;
41+
this.position = position;
42+
}
43+
44+
/**
45+
* Gets the event source.
46+
* @return the initial value
47+
*/
48+
public Object getSource() {
49+
return source;
50+
}
51+
52+
/**
53+
* Gets the text position.
54+
* @return the initial value
55+
*/
56+
public int getPosition() {
57+
return position;
58+
}
59+
}

src/org/opensolaris/opengrok/analysis/SymbolMatchedListener.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ public interface SymbolMatchedListener {
3737
* @param evt the event
3838
*/
3939
void symbolMatched(SymbolMatchedEvent evt);
40+
41+
/**
42+
* Receives an event instance.
43+
* @param evt the event
44+
*/
45+
void sourceCodeSeen(SourceCodeSeenEvent evt);
4046
}

src/org/opensolaris/opengrok/analysis/Xrefer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public interface Xrefer extends Resettable {
4747
*/
4848
int getLineNumber();
4949

50+
/**
51+
* Gets the document physical lines-of-code count.
52+
* @return a number greater than or equal to 0
53+
*/
54+
int getLOC();
55+
5056
void setAnnotation(Annotation annotation);
5157

5258
/**

0 commit comments

Comments
 (0)