Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] indexer? hierarchy? querying problem

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

On Tue, Jan 19, 2016 at 9:35 AM, Jan Baeyens <jan@xxxxxxxxxx> wrote:
That would be cool.
Do you have a pointer to similar code?

update on Nathan track
To get this type of information, you need to get an AST for the file that
contains the code. You can get one with ITranslationUnit.getAST().
I got to the file that contains the code (setup) using the indexer. fighting ITranslationUnit.getAST() right now.

Best regards
Jantje

Op 19/01/2016 om 14:43 schreef Alena Laskavaia:
I think we have some checkers in codan that will look for certain function and validate their arguments, which would be very similar code.

On Tue, Jan 19, 2016 at 6:25 AM, Jan Baeyens <jan@xxxxxxxxxx> wrote:
Nate
Thanks for the answer. From what I understand from your mail it looks perfectly like what I need and I'll try to implement this.
I may have more questions but it'll probably take time as there are plenty of gaps in my understanding I need to fill in.

Best regards
Jantje

Op 19/01/2016 om 8:27 schreef Nathan Ridge:

What I have in its simplest form is this as source code to be parsed
void setup()
{
      Serial.begin(123);
}
  What I need to get as result for this case is 123.
  I didn't get closer than this code
String variableName = "Serial";
   IIndexBinding[] bindings =
index.findBindings(variableName.toCharArray(),
IndexFilter.ALL_DECLARED_OR_IMPLICIT, new NullProgressMonitor());
When variableName is "Serial" or "setup" I get a list of bindings I can
inspect; but how do I get this for "Serial.begin"?
Generally speaking, the index only stores information about declarations.
It doesn't store things like information about a function call _expression_
occuring inside a function body.

To get this type of information, you need to get an AST for the file that
contains the code. You can get one with ITranslationUnit.getAST().

Once you have an AST, there are several ways you can get the information
you want. I'll describe one way here:
   - Use IASTTranslationUnit.getScope() get the global scope
   - Assuming "Serial" is of some type "SerialT" which is defined in the
     global scope, use IScope.find(String, IASTTranslationUnit) to find
     the binding representing this type. It should implement ICPPClassType.
   - Use ICPPClassType.getMethods() to get a list of methods of this type.
      Iterate through the list to find the one named "begin".
   - Use IASTTranslationUnit.getReferences() on the ICPPMethod representing
     the 'begin' method to find all the places where it's called.
   - Loop through the references, and examine the surrounding AST to
      find the value you need. In your case:
        - the reference to 'begin' will be an IASTName
        - its parent should be an IASTFieldReference representing 'Serial.begin'
        - its parent should be an IASTFunctionCallExpression representing
          'Serial.begin(123)'
        - the function call _expression_ will have an argument _expression_ of type
           IASTLiteralExpression
        - IASTLiteralExpression.getValue() will give you the '123'

Hope that helps,
Nate
                                               
_______________________________________________
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


_______________________________________________
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



_______________________________________________
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


_______________________________________________
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



_______________________________________________
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


Back to the top