Skip to content

Commit beaa4aa

Browse files
committed
improve cppcheck
1 parent cfcd6c3 commit beaa4aa

File tree

4 files changed

+41
-40
lines changed

4 files changed

+41
-40
lines changed

app/src/main/java/com/duy/ccppcompiler/compiler/analyze/CppCheckAnalyzer.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@
3131
import com.duy.ccppcompiler.compiler.shell.Shell;
3232
import com.duy.common.DLog;
3333
import com.duy.ide.diagnostic.DiagnosticPresenter;
34-
import com.duy.ide.diagnostic.model.Message;
3534
import com.duy.ide.editor.view.IEditAreaView;
3635
import com.jecelyin.editor.v2.Preferences;
3736
import com.jecelyin.editor.v2.editor.IEditorDelegate;
3837
import com.jecelyin.editor.v2.io.LocalFileWriter;
3938

4039
import java.io.File;
4140
import java.io.IOException;
42-
import java.util.ArrayList;
4341

4442
;
4543

@@ -126,7 +124,7 @@ public void afterTextChanged(Editable s) {
126124
}
127125

128126
@SuppressLint("StaticFieldLeak")
129-
private static class AnalyzeTask extends AsyncTask<Void, Void, ArrayList<Message>> {
127+
private static class AnalyzeTask extends AsyncTask<Void, Void, String> {
130128

131129
private final Context mContext;
132130
private final DiagnosticPresenter mDiagnosticPresenter;
@@ -139,7 +137,13 @@ public AnalyzeTask(Context mContext, IEditorDelegate delegate, DiagnosticPresent
139137
}
140138

141139
@Override
142-
protected ArrayList<Message> doInBackground(Void... voids) {
140+
protected void onPreExecute() {
141+
super.onPreExecute();
142+
mDiagnosticPresenter.clear();
143+
}
144+
145+
@Override
146+
protected String doInBackground(Void... voids) {
143147
IEditAreaView editText = mEditorDelegate.getEditText();
144148

145149
File originFile = mEditorDelegate.getDocument().getFile();
@@ -183,11 +187,7 @@ protected ArrayList<Message> doInBackground(Void... voids) {
183187
}
184188

185189
if (DLog.DEBUG) DLog.d(TAG, "result = " + result);
186-
String message = result.getMessage().replace(tmpFile.getAbsolutePath(), originFile.getAbsolutePath());
187-
CppCheckOutputParser parser = new CppCheckOutputParser();
188-
parser.parse(message);
189-
190-
return null;
190+
return result.getMessage().replace(tmpFile.getAbsolutePath(), originFile.getAbsolutePath());
191191
}
192192

193193
private File getCppCheckTmpDir() {
@@ -199,10 +199,10 @@ private File getCppCheckTmpDir() {
199199
}
200200

201201
@Override
202-
protected void onPostExecute(ArrayList<Message> messages) {
203-
super.onPostExecute(messages);
204-
if (messages != null && !isCancelled()) {
205-
mDiagnosticPresenter.setMessages(messages);
202+
protected void onPostExecute(String message) {
203+
super.onPostExecute(message);
204+
if (message != null && !isCancelled()) {
205+
mDiagnosticPresenter.onNewMessage(message);
206206
}
207207
}
208208
}

app/src/main/java/com/duy/ccppcompiler/compiler/analyze/CppCheckOutputParser.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
import android.support.annotation.NonNull;
2121

2222
import com.duy.ide.diagnostic.model.Message;
23+
import com.duy.ide.diagnostic.model.SourceFilePosition;
24+
import com.duy.ide.diagnostic.model.SourcePosition;
2325
import com.duy.ide.diagnostic.parser.PatternAwareOutputParser;
2426
import com.duy.ide.diagnostic.util.OutputLineReader;
2527
import com.duy.ide.logging.ILogger;
2628

29+
import java.io.File;
2730
import java.util.List;
31+
import java.util.regex.Matcher;
2832
import java.util.regex.Pattern;
2933

3034
/**
@@ -39,36 +43,31 @@
3943

4044
public class CppCheckOutputParser implements PatternAwareOutputParser {
4145

42-
public static final String TEMPLATE = "--template=\"{file}:{line}:{severity}:{message}\"";
43-
private static final Pattern TEMPLATE_PATTERN = Pattern.compile("^(\\S+):([0-9]+):([^:]+):(.*)");
46+
public static final String TEMPLATE = "--template=\"analysis:{file}:{line}:{severity}:{message}\"";
47+
private static final Pattern PATTERN = Pattern.compile("^analysis:(\\S+):([0-9]+):([^:]+):(.*)");
4448

4549
public CppCheckOutputParser() {
4650
}
4751

48-
public void parse(String inputData) {
49-
// try {
50-
// StringReader stringReader = new StringReader(inputData);
51-
// LineNumberReader lineReader = new LineNumberReader(stringReader);
52-
// String line;
53-
// while ((line = lineReader.readLine()) != null) {
54-
// Matcher matcher = TEMPLATE_PATTERN.matcher(line);
55-
// if (matcher.find()) {
56-
// String filePath = matcher.group(1);
57-
// int lineNumber = Integer.parseInt(matcher.group(2));
58-
// Kind type = DiagnosticFactory.createType(matcher.group(3));
59-
// String message = matcher.group(4);
60-
// Message diagnostic = DiagnosticFactory.create(type, filePath, lineNumber, Message.NOPOS, message);
61-
// diagnosticsCollector.report(diagnostic);
62-
// }
63-
// }
64-
// } catch (Exception e) {
65-
// //should not happened
66-
// e.printStackTrace();
67-
// }
68-
}
69-
7052
@Override
71-
public boolean parse(@NonNull String line, @NonNull OutputLineReader reader, @NonNull List<Message> messages, @NonNull ILogger logger) {
53+
public boolean parse(@NonNull String line, @NonNull OutputLineReader reader, @NonNull List<Message> messages,
54+
@NonNull ILogger logger) {
55+
Matcher matcher = PATTERN.matcher(line);
56+
try {
57+
58+
if (matcher.find()) {
59+
File file = new File(matcher.group(1));
60+
int lineNumber = Integer.parseInt(matcher.group(2));
61+
Message.Kind kind = Message.Kind.ERROR;
62+
String text = matcher.group(4);
63+
Message message = new Message(kind, text,
64+
new SourceFilePosition(file, new SourcePosition(lineNumber - 1, -1, -1)));
65+
messages.add(message);
66+
return true;
67+
}
68+
} catch (Exception e) {
69+
e.printStackTrace();
70+
}
7271
return false;
7372
}
7473
}

app/src/main/res/xml/preferences_cppcheck.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
android:title="--enable=performance" />
4242

4343
<CheckBoxPreference
44-
android:defaultValue="true"
44+
android:defaultValue="false"
45+
android:enabled="false"
4546
android:key="@string/pref_key_cpp_check_information"
4647
android:summary="enable information messages"
4748
android:title="--enable=information" />

lib-n-ide/src/main/java/com/nakama/arraypageradapter/ArrayFragmentStatePagerAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ private Fragment findFragmentByItem(Object item) {
356356
}
357357

358358
private void setTags(List<IdentifiedItem<T>> items) {
359-
for (int i = 0; i < mFragments.size(); i++) {
359+
final int max = Math.max(items.size(), mFragments.size());
360+
for (int i = 0; i < max; i++) {
360361
Fragment fragment = mFragments.get(i);
361362
if (fragment != null && fragment.getView() != null) {
362363
fragment.getView().setTag(R.id.avpa_view_tag_key, items.get(i));

0 commit comments

Comments
 (0)