Esempio n. 1
0
static void PrintCursor(CXCursor Cursor) {
  if (clang_isInvalid(Cursor.kind)) {
    CXString ks = clang_getCursorKindSpelling(Cursor.kind);
    printf("Invalid Cursor => %s", clang_getCString(ks));
    clang_disposeString(ks);
  }
  else {
    CXString string, ks;
    CXCursor Referenced;
    unsigned line, column;

    ks = clang_getCursorKindSpelling(Cursor.kind);
    string = clang_getCursorSpelling(Cursor);
    printf("%s=%s", clang_getCString(ks),
                    clang_getCString(string));
    clang_disposeString(ks);
    clang_disposeString(string);

    Referenced = clang_getCursorReferenced(Cursor);
    if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
      CXSourceLocation Loc = clang_getCursorLocation(Referenced);
      clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
      printf(":%d:%d", line, column);
    }

    if (clang_isCursorDefinition(Cursor))
      printf(" (Definition)");
  }
}
Esempio n. 2
0
enum CXChildVisitResult visitor(CXCursor cursor, CXCursor parent, CXClientData data)
{
  enum CXCursorKind cKind = clang_getCursorKind(cursor);
  CXString nameString = clang_getCursorDisplayName(cursor);
  CXString typeString = clang_getCursorKindSpelling(cKind);
  printf("Name:%s, Kind:%s\n", clang_getCString(nameString), clang_getCString(typeString));
  clang_disposeString(nameString);
  clang_disposeString(typeString);
  return CXChildVisit_Continue;
}
Esempio n. 3
0
void PrintDiagnostic(CXDiagnostic Diagnostic) {
  FILE *out = stderr;
  CXFile file;
  CXString Msg;
  unsigned display_opts = CXDiagnostic_DisplaySourceLocation
    | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
  unsigned i, num_fixits;

  if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
    return;

  Msg = clang_formatDiagnostic(Diagnostic, display_opts);
  fprintf(stderr, "%s\n", clang_getCString(Msg));
  clang_disposeString(Msg);

  clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
                                 &file, 0, 0, 0);
  if (!file)
    return;

  num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
  for (i = 0; i != num_fixits; ++i) {
    CXSourceRange range;
    CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
    CXSourceLocation start = clang_getRangeStart(range);
    CXSourceLocation end = clang_getRangeEnd(range);
    unsigned start_line, start_column, end_line, end_column;
    CXFile start_file, end_file;
    clang_getInstantiationLocation(start, &start_file, &start_line,
                                   &start_column, 0);
    clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
    if (clang_equalLocations(start, end)) {
      /* Insertion. */
      if (start_file == file)
        fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
                clang_getCString(insertion_text), start_line, start_column);
    } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
      /* Removal. */
      if (start_file == file && end_file == file) {
        fprintf(out, "FIX-IT: Remove ");
        PrintExtent(out, start_line, start_column, end_line, end_column);
        fprintf(out, "\n");
      }
    } else {
      /* Replacement. */
      if (start_file == end_file) {
        fprintf(out, "FIX-IT: Replace ");
        PrintExtent(out, start_line, start_column, end_line, end_column);
        fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
      }
      break;
    }
    clang_disposeString(insertion_text);
  }
}
Esempio n. 4
0
QASTField::QASTField(
        QAnnotatedTokenSet *tokenSet,
        QSourceLocation* cursorLocation,
        QSourceLocation* rangeStartLocation,
        QSourceLocation* rangeEndLocation,
        QASTNode* parent)

    : QASTNode("field", tokenSet, cursorLocation, rangeStartLocation, rangeEndLocation, parent){

    // Get Identifier
    // --------------

    CXString id = clang_getCursorSpelling(tokenSet->cursor());
    setIdentifier(clang_getCString(id));

    // Get Field Type
    // ---------------

    CXType type           = clang_getCursorType(tokenSet->cursor());
    CXString typeSpelling = clang_getTypeSpelling(type);
    m_fieldType           = clang_getCString(typeSpelling);
    clang_disposeString(typeSpelling);

    // Determine field type
    // --------------------

    if ( type.kind == CXType_Unexposed || type.kind == CXType_Invalid || m_fieldType.contains("int") ){
        m_fieldType = "";

        bool doubleColonFlag = false;
        for ( QAnnotatedTokenSet::Iterator it = tokenSet->begin(); it != tokenSet->end(); ++it ){

            CXToken t              = (*it)->token().token;
            CXString tSpelling     = clang_getTokenSpelling(tokenSet->translationUnit(), t);
            const char* tCSpelling = clang_getCString(tSpelling);
            CXTokenKind tKind      = clang_getTokenKind(t);

            if ( tKind == CXToken_Identifier && std::string(clang_getCString(id)) == tCSpelling ){
                break;
            } else if ( tKind == CXToken_Punctuation && std::string("::") == tCSpelling ){
                doubleColonFlag = true;
                m_fieldType    += tCSpelling;
            } else if ( ( tKind == CXToken_Identifier || tKind == CXToken_Keyword ) &&
                        !m_fieldType.isEmpty() && !doubleColonFlag ){
                m_fieldType    += QString(" ") + tCSpelling;
            } else {
                doubleColonFlag = false;
                m_fieldType    += tCSpelling;
            }
        }
    }

    clang_disposeString(id);
}
Esempio n. 5
0
QString getQString(const CXString &cxString, bool disposeCXString)
{
    QString s = QString::fromUtf8(clang_getCString(cxString));
    if (disposeCXString)
        clang_disposeString(cxString);
    return s;
}
/* Handle syntax checking request, message format:
       source_length: [#src_length#]
       <# SOURCE CODE #>
*/
void completion_doSyntaxCheck(completion_Session *session, FILE *fp)
{
    unsigned int i_diag = 0, n_diag;
    CXDiagnostic diag;
    CXString     dmsg;

    /* get a copy of fresh source file */
    completion_readSourcefile(session, fp);

    /* reparse the source to retrieve diagnostic message */
    completion_reparseTranslationUnit(session);

    /* dump all diagnostic messages to fp */
    n_diag = clang_getNumDiagnostics(session->cx_tu);
    for ( ; i_diag < n_diag; i_diag++)
    {
        diag = clang_getDiagnostic(session->cx_tu, i_diag);
        dmsg = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
        fprintf(stdout, "%s\n", clang_getCString(dmsg));
        clang_disposeString(dmsg);
        clang_disposeDiagnostic(diag);
    }

    fprintf(stdout, "$"); fflush(stdout);    /* end of output */
}
Esempio n. 7
0
void print_completion_string(CXCompletionString completion_string, FILE *file) {
  int I, N;

  N = clang_getNumCompletionChunks(completion_string);
  for (I = 0; I != N; ++I) {
    CXString text;
    const char *cstr;
    enum CXCompletionChunkKind Kind
      = clang_getCompletionChunkKind(completion_string, I);

    if (Kind == CXCompletionChunk_Optional) {
      fprintf(file, "{Optional ");
      print_completion_string(
                clang_getCompletionChunkCompletionString(completion_string, I),
                              file);
      fprintf(file, "}");
      continue;
    }

    text = clang_getCompletionChunkText(completion_string, I);
    cstr = clang_getCString(text);
    fprintf(file, "{%s %s}",
            clang_getCompletionChunkKindSpelling(Kind),
            cstr ? cstr : "");
    clang_disposeString(text);
  }

}
Esempio n. 8
0
std::ostream& operator<<( std::ostream& os, CXType type )
{
    auto typestr = clang_getTypeSpelling( type );
    os << "Type: " << clang_getCString( typestr ) << "\n";
    clang_disposeString( typestr );
    return os;
}
Esempio n. 9
0
void
ast_json::record_token(
    json_t& obj, CXToken token, CXTranslationUnit translation_unit)
{
    // Record token kind
    switch (clang_getTokenKind(token)) {
      case CXToken_Punctuation:
          obj[lex_token::KIND] = "punctuation";
          break;
      case CXToken_Keyword:
          obj[lex_token::KIND] = "keyword";
          break;
      case CXToken_Identifier:
          obj[lex_token::KIND] = "identifier";
          break;
      case CXToken_Literal:
          obj[lex_token::KIND] = "literal";
          break;
      case CXToken_Comment:
          obj[lex_token::KIND] = "comment";
          break;
    }

    // Get attributes
    CXString spelling = clang_getTokenSpelling(translation_unit, token);
    CXSourceRange extent = clang_getTokenExtent(translation_unit, token);

    obj[lex_token::DATA] = std::string(clang_getCString(spelling));
    record_extent(obj, extent);

    // Reclaim memory
    clang_disposeString(spelling);
}
Esempio n. 10
0
static const char* GetCursorSource(CXCursor Cursor) {
  CXSourceLocation Loc = clang_getCursorLocation(Cursor);
  CXString source;
  CXFile file;
  clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
  source = clang_getFileName(file);
  if (!clang_getCString(source)) {
    clang_disposeString(source);
    return "<invalid loc>";
  }
  else {
    const char *b = basename(clang_getCString(source));
    clang_disposeString(source);
    return b;
  }
}
Esempio n. 11
0
static int code_completion_results_cmp(CXCompletionResult *r1,
				       CXCompletionResult *r2)
{
	int prio1 = clang_getCompletionPriority(r1->CompletionString);
	int prio2 = clang_getCompletionPriority(r2->CompletionString);
	if (prio1 != prio2)
		return prio1 - prio2;

	CXString r1t = get_result_typed_text(r1);
	CXString r2t = get_result_typed_text(r2);
	int cmp = strcmp(clang_getCString(r1t),
			 clang_getCString(r2t));
	clang_disposeString(r1t);
	clang_disposeString(r2t);
	return cmp;
}
Esempio n. 12
0
json_t* render_string_or_null(CXString s) {
  const char* str = clang_getCString(s);
  json_t* js;
  if (str && strlen(str) > 0) js = json_string(str);
  else js = json_null();
  clang_disposeString(s);
  return js;
}
Esempio n. 13
0
static void printString(const char *name, CXString string)
{
    const char *cstr = clang_getCString(string);
    if (cstr && *cstr) {
        printf("%s: %s ", name, cstr);
    }
    clang_disposeString(string);
}
Esempio n. 14
0
static void printVersion() {
    // do not change the format for the first line, external programs should be
    // able to rely on it
    std::cout << "irony-server version " IRONY_PACKAGE_VERSION "\n";

    CXString cxVersionString = clang_getClangVersion();
    std::cout << clang_getCString(cxVersionString) << "\n";
    clang_disposeString(cxVersionString);
}
/** Callback function called for each include in a translation unit */
static void inclusionVisitor(CXFile includedFile, CXSourceLocation * , unsigned, CXClientData clientData)
{
   QSet<QString> *fileDict = reinterpret_cast<QSet<QString> *>(clientData);

   CXString incFileName = clang_getFileName(includedFile);
 
   fileDict->insert(clang_getCString(incFileName));
   clang_disposeString(incFileName);
}
Esempio n. 16
0
std::ostream& operator<<( std::ostream &os, CXCursor cursor )
{
    auto spelling = clang_getCursorKindSpelling( cursor.kind );
    auto morespell = clang_getCursorSpelling( cursor );
  
    os << clang_getCString( spelling ) << ":"
       << clang_getCString( morespell ) << " ";

    clang_disposeString( spelling );
    clang_disposeString( morespell );

    auto refcursor = clang_getCursorReferenced( cursor );
    if ( clang_equalCursors( refcursor, clang_getNullCursor() )||
	 clang_equalCursors( refcursor, cursor ) )
	return os;
    
    return os << " [ " << refcursor << " ] ";
}
Esempio n. 17
0
string CursorHelper::getFileName(CXCursor cursor)
{
  CXFile file;
  clang_getSpellingLocation(clang_getCursorLocation(cursor), &file, 0, 0, 0);
  CXString fileStr = clang_getFileName(file);
  string fileName(clang_getCString(fileStr));
  clang_disposeString(fileStr);
  return fileName;
}
Esempio n. 18
0
int main(int argc, char *argv[])
{
  auto index = clang_createIndex(0, 0);
  auto options = clang_defaultEditingTranslationUnitOptions();
  char const *args[] = { "-x", "c++", "-std=c++11" };
  auto arg_count = sizeof( args ) / sizeof( *args );
  filename = argv[1];
  
  CXUnsavedFile *unsaved_files = NULL;
  auto unsaved_file_count = 0;
  
  tu = clang_parseTranslationUnit(index, filename.c_str(), args, arg_count,
				  unsaved_files, unsaved_file_count,
				  options );
  
  if ( !tu ) {
    std::cout << "Translation Unit Parse Failed!\n";
    return -1;
  }

  std::stringstream ss( argv[2] );
  int line, col;

  ss >> line;
  ss.get();
  ss >> col;
  std::cout << "Hello " << line << ":" << col << "\n";

  auto file = clang_getFile( tu, filename.c_str() );
  auto location = clang_getLocation( tu, file, line, col );

  
  clang_visitChildren( clang_getTranslationUnitCursor( tu ), visitor,
		       reinterpret_cast<CXClientData>(0) );
  
  auto cursor = clang_getCursor( tu, location );
  auto refcursor = clang_getCursorReferenced( cursor );
  auto rrefcursor = clang_getCursorReferenced( refcursor );
  auto arf = clang_getTypeKindSpelling( clang_getCursorType( cursor ).kind );
  auto foo = clang_getCanonicalCursor( cursor );
  auto semparent = clang_getCursorSemanticParent( cursor );
  auto lexparent = clang_getCursorLexicalParent( cursor );

  std::cout << cursor << "\n";
  std::cout << refcursor << "\n";
  std::cout << rrefcursor << "\n";
  std::cout << clang_getCString(arf) << "\n";
  std::cout << foo << "\n";
  std::cout << "Parent: " << semparent << "\n";
  std::cout << "LexParent: " << lexparent << "\n";
  
  //clang_visitChildren( semparent, visitor, reinterpret_cast<CXClientData>(0) );
  clang_disposeString( arf );

  return 0;
}
Esempio n. 19
0
static enum CXChildVisitResult
ide_clang_service_build_index_visitor (CXCursor     cursor,
                                       CXCursor     parent,
                                       CXClientData user_data)
{
  IndexRequest *request = user_data;
  enum CXCursorKind kind;
  const gchar *style_name = NULL;

  g_assert (request != NULL);

  kind = clang_getCursorKind (cursor);

  switch ((int)kind)
    {
    case CXCursor_TypedefDecl:
    case CXCursor_TypeAliasDecl:
      style_name = IDE_CLANG_HIGHLIGHTER_TYPE;
      break;

    case CXCursor_FunctionDecl:
      style_name = IDE_CLANG_HIGHLIGHTER_FUNCTION_NAME;
      break;

    case CXCursor_EnumDecl:
      style_name = IDE_CLANG_HIGHLIGHTER_ENUM_NAME;
      clang_visitChildren (cursor,
                           ide_clang_service_build_index_visitor,
                           user_data);
      break;

    case CXCursor_EnumConstantDecl:
      style_name = IDE_CLANG_HIGHLIGHTER_ENUM_NAME;
      break;

    case CXCursor_MacroDefinition:
      style_name = IDE_CLANG_HIGHLIGHTER_MACRO_NAME;
      break;

    default:
      break;
    }

  if (style_name != NULL)
    {
      CXString cxstr;
      const gchar *word;

      cxstr = clang_getCursorSpelling (cursor);
      word = clang_getCString (cxstr);
      ide_highlight_index_insert (request->index, word, (gpointer)style_name);
      clang_disposeString (cxstr);
    }

  return CXChildVisit_Continue;
}
Esempio n. 20
0
std::string CXStringToString( CXString text ) {
  std::string final_string;

  if ( !text.data )
    return final_string;

  final_string = std::string( clang_getCString( text ) );
  clang_disposeString( text );
  return final_string;
}
Esempio n. 21
0
static std::string cxStringToStd(CXString cxString) {
  std::string stdStr;

  if (const char *cstr = clang_getCString(cxString)) {
    stdStr = cstr;
  }

  clang_disposeString(cxString);
  return stdStr;
}
std::string Generator::stringize(const CXString &string) const
{
    const char *chars = clang_getCString(string);
    if (!chars)
        return std::string();

    std::string ret(chars);
    clang_disposeString(string);
    return ret;
}
Esempio n. 23
0
int
main(int argc, char *argv[])
{
    CXString str;
    str = clang_getClangVersion();
    const char * const tmp = clang_getCString(str);
    printf("%s\n", tmp);
    clang_disposeString(str);  // ???? should we do this here.
    return(1);
}
Esempio n. 24
0
std::ostream& operator<<( std::ostream& os, CXSourceLocation loc )
{
    CXFile cxfile;
    unsigned line, col, off;
    clang_getFileLocation( loc, &cxfile, &line, &col, &off );
    auto filestr = clang_getFileName( cxfile );
    os << clang_getCString( filestr ) << ":" << line << ":" << col;
    clang_disposeString( filestr );
    return os;
}
Esempio n. 25
0
static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
                                                   CXCursor Parent,
                                                   CXClientData ClientData) {
  const char *startBuf, *endBuf;
  unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
  CXCursor Ref;
  VisitorData *Data = (VisitorData *)ClientData;

  if (Cursor.kind != CXCursor_FunctionDecl ||
      !clang_isCursorDefinition(Cursor))
    return CXChildVisit_Continue;

  clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
                                       &startLine, &startColumn,
                                       &endLine, &endColumn);
  /* Probe the entire body, looking for both decls and refs. */
  curLine = startLine;
  curColumn = startColumn;

  while (startBuf < endBuf) {
    CXSourceLocation Loc;
    CXFile file;
    CXString source;

    if (*startBuf == '\n') {
      startBuf++;
      curLine++;
      curColumn = 1;
    } else if (*startBuf != '\t')
      curColumn++;

    Loc = clang_getCursorLocation(Cursor);
    clang_getInstantiationLocation(Loc, &file, 0, 0, 0);

    source = clang_getFileName(file);
    if (clang_getCString(source)) {
      CXSourceLocation RefLoc
        = clang_getLocation(Data->TU, file, curLine, curColumn);
      Ref = clang_getCursor(Data->TU, RefLoc);
      if (Ref.kind == CXCursor_NoDeclFound) {
        /* Nothing found here; that's fine. */
      } else if (Ref.kind != CXCursor_FunctionDecl) {
        printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
               curLine, curColumn);
        PrintCursor(Ref);
        printf("\n");
      }
    }
    clang_disposeString(source);
    startBuf++;
  }

  return CXChildVisit_Continue;
}
Esempio n. 26
0
enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
                                   CXClientData ClientData) {
  VisitorData *Data = (VisitorData *)ClientData;
  if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
    CXString USR = clang_getCursorUSR(C);
    if (!clang_getCString(USR)) {
      clang_disposeString(USR);
      return CXChildVisit_Continue;
    }
    printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C),
                           clang_getCString(USR));
    PrintCursorExtent(C);
    printf("\n");
    clang_disposeString(USR);

    return CXChildVisit_Recurse;
  }

  return CXChildVisit_Continue;
}
Esempio n. 27
0
/** Callback function called for each include in a translation unit */
static void inclusionVisitor(CXFile includedFile,
                             CXSourceLocation* /*inclusionStack*/,
                             unsigned /*includeLen*/,
                             CXClientData clientData)
{
  QDict<void> *fileDict = (QDict<void> *)clientData;
  CXString incFileName = clang_getFileName(includedFile);
  //printf("--- file %s includes %s\n",fileName,clang_getCString(incFileName));
  fileDict->insert(clang_getCString(incFileName),(void*)0x8);
  clang_disposeString(incFileName);
}
Esempio n. 28
0
void print_completion_result(CXCompletionResult *completion_result,
                             CXClientData client_data) {
  FILE *file = (FILE *)client_data;
  CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);

  fprintf(file, "%s:", clang_getCString(ks));
  clang_disposeString(ks);

  print_completion_string(completion_result->CompletionString, file);
  fprintf(file, "\n");
}
Esempio n. 29
0
ClangString &ClangString::operator=(ClangString &&other)
{
    if (this != &other) {
        clang_disposeString(cxString);
        cxString = std::move(other.cxString);
        other.cxString.data = nullptr;
        other.cxString.private_flags = 0;
    }

    return *this;
}
void QAnnotatedTokenSet::dump(std::string &str){
    Q_D(QAnnotatedTokenSet);
    CXString displayName = clang_getCursorDisplayName(d->cursor);
    str.append("Cursor :");
    str.append(clang_getCString(displayName));
    str.append("\n Tokens :");
    clang_disposeString(displayName);

    for ( QAnnotatedTokenSet::Iterator it = begin(); it != end(); ++it ){
        CXString tokenString           = clang_getTokenSpelling(d->translationUnit, (*it)->token().token);
        CXSourceLocation tokenLocation = clang_getTokenLocation(d->translationUnit, (*it)->token().token);
        unsigned int column, line;
        clang_getSpellingLocation(tokenLocation, 0, &line, &column, 0);
        std::stringstream stream;
        stream << " (" << line << "," << column << ") \"";
        str.append(stream.str() + clang_getCString(tokenString) + "\"");
        clang_disposeString(tokenString);
    }
    str.append("\n");
}