Autocompletion

Arduino IDE will get code completion and error check using eclipse's CDT.

libclang

I decided to use libclang for AST.

Here some links to fix various problems I've found so far.

I use libclang without AVR support because I don't need an error messages at this time, only source tokenizer.

Current status

Sometimes in future this part will go to the separate project. No reason to create C/C++ project environment to compile only Arduinos.

I created test environment using node-libclang, processed ino file and Arduino headers. To make it work, I need to add dll/so/dylib to library search path.

Code below produce some understandable and good looking output, but at this time external function prototypes from *.h not found in output.

all files

var libclang = require('libclang');
var Index = libclang.Index;
var Cursor = libclang.Cursor;
var TranslationUnit = libclang.TranslationUnit;

var index = new Index(true, true);

var source = "/var/folders/r4/d4l8c_ts4rsdc670pdkbtr0m0000gp/T/Sensor-cuwire-01189b11/Sensor.cpp";

var tu = TranslationUnit.fromSource(
    index,
    source, [
//      "-fsyntax-only",
        "-nostdinc",
//      "-nostdlibinc",
//      "-nobuiltininc",
// less warnings if platform/hardware flags not defined
        "-MMD",
        "-xc++",
        "-DF_CPU=16000000L",
        "-DARDUINO=158",
        "-DARDUINO_AVR_PRO",
        "-DARDUINO_ARCH_AVR",
        "-D__AVR_ATmega328P__",
        "-D__AVR__",
// end less warnings
        "-I/Applications/devel/Arduino.app/Contents/Java/hardware/tools/avr/lib/gcc/avr/4.8.1/include/",
        "-I/Applications/devel/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/",
        "-I/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino",
        "-I/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/variants/eightanaloginputs",
        "-I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire",
        "-I/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/EEPROM",
        "-I/Users/apla/Documents/Arduino/libraries/Time",
        "-I/Users/apla/Documents/Arduino/libraries/DS3231RTC",
        "-I/Users/apla/Documents/Arduino/libraries/EtherCard",
        "-I/Users/apla/Documents/Arduino/libraries/JeeLib"
    ]
);

//console.log (Cursor);

tu.cursor.visitChildren(function (parent) {
//  console.log (parent);
//  if (this.spelling === "analogRead") {
    console.log (this.spelling, this.kind, this.location.presumedLocation);
//  }
    switch (this.kind) {
        case Cursor.FunctionDecl:
            console.log('function', this.spelling, this.location.presumedLocation);
            // TODO: try/catch?
            var self = this;
//          this.visitChildren (function (parent) {
//              console.log ('function', self.spelling, 'has child', this.spelling);
//          });
            //console.log('displayName', this.displayName);
//          console.log (this);
//          process.exit(0);
            break;
        case Cursor.MacroDefinition:
//          console.log('macro', this.spelling);
            break;
        case Cursor.InclusionDirective:
//          console.log('inclusion', this.spelling);
            break;
    }
    return Cursor.Continue;
});

index.dispose();
//tu.dispose();

prototype of analogRead is declared between lines 115 and 150 of Arduino.h, but nothing found

bitClear 501 { filename: '/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h',
    line: 105,
    column: 9 }
bitWrite 501 { filename: '/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h',
    line: 106,
    column: 9 }
_NOP 501 { filename: '/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h',
    line: 110,
    column: 9 }
bit 501 { filename: '/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h',
    line: 115,
    column: 9 }
analogInPinToBit 501 { filename: '/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h',
    line: 150,
    column: 9 }
PROGMEM 502 { filename: '/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h',
    line: 154,
    column: 23 }
PROGMEM 502 { filename: '/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h',
    line: 155,
    column: 23 }
PROGMEM 502 { filename: '/Applications/devel/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h',
    line: 156,
    column: 23 }

Autocompletion links

binaries download


nodejs:

  • https://github.com/tbehrsin/node-llvm
  • https://github.com/tjfontaine/node-libclang - best one, not working

python:

  • https://mikeash.com/pyblog/friday-qa-2014-01-24-introduction-to-libclang.html — good one!
  • http://szelei.me/code-generator/ — good one!

notes:

  • http://stackoverflow.com/questions/12026551/how-to-find-out-whether-a-member-function-is-const-or-volatile-with-libclang — using USR
  • http://lists.cs.uiuc.edu/pipermail/cfe-dev/2013-February/028106.html
  • http://stackoverflow.com/questions/20280744/libclang-returns-too-much-info-about-function-declarations — function return type
  • http://clang-developers.42468.n3.nabble.com/libclang-inspecting-function-parameters-with-default-arguments-td4037675.html — function declaration default value

clang/llvm for avr

  • https://github.com/dylanmckay/avr-llvm
  • http://stackoverflow.com/questions/19006000/how-to-compile-clang-to-use-as-compiler-for-avr
  • https://github.com/4ntoine/clang/tree/avr-support

legacy:

  • https://github.com/sushihangover/llvm-avr

atom autocomplete clang

  • https://atom.io/packages/autocomplete-clang