Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a36ba85

Browse files
committedMay 29, 2017
Introducing ArduinoDiagnosticConsumer class
This class will be extended in later commits.
1 parent 16b2633 commit a36ba85

File tree

4 files changed

+129
-45
lines changed

4 files changed

+129
-45
lines changed
 

‎ArduinoDiagnosticConsumer.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* This file is part of arduino-preprocessor.
3+
*
4+
* Copyright 2017 BCMI LABS SA
5+
*
6+
* arduino-preprocessor is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
#include "ArduinoDiagnosticConsumer.h"
31+
#include "CommandLine.h"
32+
33+
void ArduinoDiagnosticConsumer::collectUndeclaredIdentifiersIn(IdentifiersList &list) {
34+
undeclaredIdentifiersList = &list;
35+
}
36+
37+
void ArduinoDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level level, const Diagnostic& info) {
38+
// DiagnosticConsumer::HandleDiagnostic(level, info);
39+
40+
if (undeclaredIdentifiersList && level == DiagnosticsEngine::Level::Error) {
41+
const SourceManager &sm = info.getSourceManager();
42+
const SourceLocation &loc = info.getLocation();
43+
const SourceLocation &sl = sm.getSpellingLoc(loc);
44+
if (debugOutput) {
45+
outs() << sm.getSpellingLineNumber(sl) << ":" << sm.getSpellingColumnNumber(sl) << " ";
46+
}
47+
48+
unsigned id = info.getID();
49+
if (id == 3441 /* use of undeclared identifier */) {
50+
// It seems that the only way to retrieve the undeclared symbol
51+
// is to print it as a localization string.
52+
const char *fmt = "%0";
53+
SmallString<100> outArg;
54+
info.FormatDiagnostic(fmt, fmt + 2, outArg);
55+
// we should also remove quotes as well...
56+
StringRef identifier = outArg.substr(1, outArg.size() - 2);
57+
if (debugOutput) {
58+
outs() << "Found undeclared identifier '" << identifier << "'\n";
59+
}
60+
61+
// Save the identifier position for later processing
62+
IdentifierLocation *m = new IdentifierLocation;
63+
m->location = FullSourceLoc(loc, sm);
64+
m->identifier = identifier.str();
65+
undeclaredIdentifiersList->push_front(m);
66+
return;
67+
}
68+
69+
if (debugOutput) {
70+
SmallString<100> outStr;
71+
info.FormatDiagnostic(outStr);
72+
outs() << "(" << id << ") " << outStr << "\n";
73+
}
74+
}
75+
}

‎ArduinoDiagnosticConsumer.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This file is part of arduino-preprocessor.
3+
*
4+
* Copyright 2017 BCMI LABS SA
5+
*
6+
* arduino-preprocessor is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
#pragma once
31+
32+
#include <clang/AST/ASTConsumer.h>
33+
34+
#include "IdentifiersList.h"
35+
36+
using namespace clang;
37+
using namespace llvm;
38+
39+
class ArduinoDiagnosticConsumer : public DiagnosticConsumer {
40+
public:
41+
42+
void collectUndeclaredIdentifiersIn(IdentifiersList &list);
43+
44+
private:
45+
IdentifiersList *undeclaredIdentifiersList = nullptr;
46+
47+
void HandleDiagnostic(DiagnosticsEngine::Level level, const Diagnostic& info) override;
48+
};

‎main.cpp

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <sstream>
4646
#include <list>
4747

48+
#include "ArduinoDiagnosticConsumer.h"
4849
#include "Config.h"
4950
#include "CommandLine.h"
5051
#include "IdentifiersList.h"
@@ -57,49 +58,6 @@ using namespace std;
5758

5859
IdentifiersList undeclaredIdentifiers;
5960

60-
class UndefinedIdentifiersCollector : public DiagnosticConsumer {
61-
62-
void HandleDiagnostic(DiagnosticsEngine::Level level, const Diagnostic& info) override {
63-
// DiagnosticConsumer::HandleDiagnostic(level, info);
64-
65-
if (level == DiagnosticsEngine::Level::Error) {
66-
const SourceManager &sm = info.getSourceManager();
67-
const SourceLocation &loc = info.getLocation();
68-
const SourceLocation &sl = sm.getSpellingLoc(loc);
69-
if (debugOutput) {
70-
outs() << sm.getSpellingLineNumber(sl) << ":" << sm.getSpellingColumnNumber(sl) << " ";
71-
}
72-
73-
unsigned id = info.getID();
74-
if (id == 3441 /* use of undeclared identifier */) {
75-
// It seems that the only way to retrieve the undeclared symbol
76-
// is to print it as a localization string.
77-
const char *fmt = "%0";
78-
SmallString<100> outArg;
79-
info.FormatDiagnostic(fmt, fmt + 2, outArg);
80-
// we should also remove quotes as well...
81-
StringRef identifier = outArg.substr(1, outArg.size() - 2);
82-
if (debugOutput) {
83-
outs() << "Found undeclared identifier '" << identifier << "'\n";
84-
}
85-
86-
// Save the identifier position for later processing
87-
IdentifierLocation *m = new IdentifierLocation;
88-
m->location = FullSourceLoc(loc, sm);
89-
m->identifier = identifier.str();
90-
undeclaredIdentifiers.push_front(m);
91-
return;
92-
}
93-
94-
if (debugOutput) {
95-
SmallString<100> outStr;
96-
info.FormatDiagnostic(outStr);
97-
outs() << "(" << id << ") " << outStr << "\n";
98-
}
99-
}
100-
}
101-
};
102-
10361
Rewriter rewriter;
10462

10563
class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
@@ -249,7 +207,10 @@ int main(int argc, const char **argv) {
249207
CommonOptionsParser optParser = doCommandLineParsing(argc, argv);
250208
ClangTool tool(optParser.getCompilations(), optParser.getSourcePathList());
251209

252-
UndefinedIdentifiersCollector dc;
210+
ArduinoDiagnosticConsumer dc;
211+
if (outputOnlyNeededPrototypes) {
212+
dc.collectUndeclaredIdentifiersIn(undeclaredIdentifiers);
213+
}
253214
tool.setDiagnosticConsumer(&dc);
254215

255216
int res = tool.run(newFrontendActionFactory<INOPreprocessAction>().get());

‎package-arduino-preprocessor.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ CXXFLAGS=`clang/bin/llvm-config --cxxflags`
108108
LDFLAGS=`clang/bin/llvm-config --ldflags`
109109
LLVMLIBS=`clang/bin/llvm-config --libs --system-libs`
110110
CLANGLIBS=`ls clang/lib/libclang*.a | sed s/.*libclang/-lclang/ | sed s/.a$//`
111-
SOURCES="main.cpp CommandLine.cpp IdentifiersList.cpp"
111+
SOURCES="main.cpp ArduinoDiagnosticConsumer.cpp CommandLine.cpp IdentifiersList.cpp"
112112
$CXX $SOURCES -o objdir/arduino-preprocessor $CXXFLAGS $LDFLAGS -Wl,--start-group $LLVMLIBS $CLANGLIBS -Wl,--end-group
113113

114114
rm -f arduino-preprocessor-${OUTPUT_VERSION}-${OUTPUT_TAG}.tar.bz2

0 commit comments

Comments
 (0)
Please sign in to comment.