After fighting AST I decided to use the indexer to find the file
containing the setup code and do a simple string parsing in that
file.
Thanks for all the help
Here is the result
private int getBaudRate(IProject iProject) {
String setupFunctionName = "setup";
String serialVariable = "Serial.begin";
ICProject curProject =
CoreModel.getDefault().getCModel().getCProject(iProject.getName());
IIndex index = null;
try {
index =
CCorePlugin.getIndexManager().getIndex(curProject);
index.acquireReadLock();
// find bindings for name
IIndexBinding[] bindings =
index.findBindings(setupFunctionName.toCharArray(),
IndexFilter.ALL_DECLARED_OR_IMPLICIT, new
NullProgressMonitor());
if (bindings.length != 1) {
// there should be just 1 setup function
return -1;
}
if (!(bindings[0] instanceof ICPPFunction)) {
return -2;// that on found binding must be a function
}
ICPPFunction setupFunc = (ICPPFunction) bindings[0];
IIndexName[] names = index.findNames(setupFunc,
org.eclipse.cdt.core.index.IIndex.FIND_DEFINITIONS);
if (names.length != 1) {
return -3;
}
String SetupFileName =
names[0].getFileLocation().getFileName();
String SetupFileContent = FileUtils.readFileToString(new
File(SetupFileName));
int serialBeginStart =
SetupFileContent.indexOf(serialVariable);
if (serialBeginStart != -1) {
int serialBeginStartbraket = SetupFileContent.indexOf("(",
serialBeginStart);
if (serialBeginStartbraket != -1) {
int serialBeginCloseBraket =
SetupFileContent.indexOf(")", serialBeginStartbraket);
if (serialBeginCloseBraket != -1) {
String baudrate =
SetupFileContent.substring(serialBeginStartbraket + 1,
serialBeginCloseBraket)
.trim();
return Integer.parseInt(baudrate);
}
}
}
} catch (CoreException | InterruptedException | IOException e)
{
e.printStackTrace();
} finally {
if (index != null) {
index.releaseReadLock();
}
}
return -1;
}
Op 19/01/2016 om 15:55 schreef Alena
Laskavaia:
ScanfFormatStringSecurityChecker
_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev
|