Пример #1
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;
}
Пример #2
0
static void print_cursor_file_scan(CXCursor cursor,
                                   unsigned start_line, unsigned start_col,
                                   unsigned end_line, unsigned end_col,
                                   const char *prefix) {
  printf("// %s: ", FileCheckPrefix);
  if (prefix)
    printf("-%s", prefix);
  PrintExtent(stdout, start_line, start_col, end_line, end_col);
  printf(" ");
  PrintCursor(cursor);
  printf("\n");
}
Пример #3
0
enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
                                                CXCursor Parent,
                                                CXClientData ClientData) {
  VisitorData *Data = (VisitorData *)ClientData;
  if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
    CXSourceLocation Loc = clang_getCursorLocation(Cursor);
    unsigned line, column;
    clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
    printf("// %s: %s:%d:%d: ", FileCheckPrefix,
           GetCursorSource(Cursor), line, column);
    PrintCursor(Cursor);
    PrintCursorExtent(Cursor);
    printf("\n");
    return CXChildVisit_Recurse;
  }

  return CXChildVisit_Continue;
}
Пример #4
0
static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
                                            CXClientData d) {
  const char *linkage = 0;

  if (clang_isInvalid(clang_getCursorKind(cursor)))
    return CXChildVisit_Recurse;

  switch (clang_getCursorLinkage(cursor)) {
    case CXLinkage_Invalid: break;
    case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
    case CXLinkage_Internal: linkage = "Internal"; break;
    case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
    case CXLinkage_External: linkage = "External"; break;
  }

  if (linkage) {
    PrintCursor(cursor);
    printf("linkage=%s\n", linkage);
  }

  return CXChildVisit_Recurse;
}
Пример #5
0
int perform_token_annotation(int argc, const char **argv) {
  const char *input = argv[1];
  char *filename = 0;
  unsigned line, second_line;
  unsigned column, second_column;
  CXIndex CIdx;
  CXTranslationUnit TU = 0;
  int errorCode;
  struct CXUnsavedFile *unsaved_files = 0;
  int num_unsaved_files = 0;
  CXToken *tokens;
  unsigned num_tokens;
  CXSourceRange range;
  CXSourceLocation startLoc, endLoc;
  CXFile file = 0;
  CXCursor *cursors = 0;
  unsigned i;

  input += strlen("-test-annotate-tokens=");
  if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
                                          &second_line, &second_column)))
    return errorCode;

  if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
    return -1;

  CIdx = clang_createIndex(0, 1);
  TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
                                                 argc - num_unsaved_files - 3,
                                                 argv + num_unsaved_files + 2,
                                                 num_unsaved_files,
                                                 unsaved_files);
  if (!TU) {
    fprintf(stderr, "unable to parse input\n");
    clang_disposeIndex(CIdx);
    free(filename);
    free_remapped_files(unsaved_files, num_unsaved_files);
    return -1;
  }
  errorCode = 0;

  file = clang_getFile(TU, filename);
  if (!file) {
    fprintf(stderr, "file %s is not in this translation unit\n", filename);
    errorCode = -1;
    goto teardown;
  }

  startLoc = clang_getLocation(TU, file, line, column);
  if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
    fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
            column);
    errorCode = -1;
    goto teardown;
  }

  endLoc = clang_getLocation(TU, file, second_line, second_column);
  if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
    fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
            second_line, second_column);
    errorCode = -1;
    goto teardown;
  }

  range = clang_getRange(startLoc, endLoc);
  clang_tokenize(TU, range, &tokens, &num_tokens);
  cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
  clang_annotateTokens(TU, tokens, num_tokens, cursors);
  for (i = 0; i != num_tokens; ++i) {
    const char *kind = "<unknown>";
    CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
    CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
    unsigned start_line, start_column, end_line, end_column;

    switch (clang_getTokenKind(tokens[i])) {
    case CXToken_Punctuation: kind = "Punctuation"; break;
    case CXToken_Keyword: kind = "Keyword"; break;
    case CXToken_Identifier: kind = "Identifier"; break;
    case CXToken_Literal: kind = "Literal"; break;
    case CXToken_Comment: kind = "Comment"; break;
    }
    clang_getInstantiationLocation(clang_getRangeStart(extent),
                                   0, &start_line, &start_column, 0);
    clang_getInstantiationLocation(clang_getRangeEnd(extent),
                                   0, &end_line, &end_column, 0);
    printf("%s: \"%s\" ", kind, clang_getCString(spelling));
    PrintExtent(stdout, start_line, start_column, end_line, end_column);
    if (!clang_isInvalid(cursors[i].kind)) {
      printf(" ");
      PrintCursor(cursors[i]);
    }
    printf("\n");
  }
  free(cursors);

 teardown:
  PrintDiagnostics(TU);
  clang_disposeTranslationUnit(TU);
  clang_disposeIndex(CIdx);
  free(filename);
  free_remapped_files(unsaved_files, num_unsaved_files);
  return errorCode;
}
Пример #6
0
int inspect_cursor_at(int argc, const char **argv) {
  CXIndex CIdx;
  int errorCode;
  struct CXUnsavedFile *unsaved_files = 0;
  int num_unsaved_files = 0;
  CXTranslationUnit TU;
  CXCursor Cursor;
  CursorSourceLocation *Locations = 0;
  unsigned NumLocations = 0, Loc;

  /* Count the number of locations. */
  while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
    ++NumLocations;

  /* Parse the locations. */
  assert(NumLocations > 0 && "Unable to count locations?");
  Locations = (CursorSourceLocation *)malloc(
                                  NumLocations * sizeof(CursorSourceLocation));
  for (Loc = 0; Loc < NumLocations; ++Loc) {
    const char *input = argv[Loc + 1] + strlen("-cursor-at=");
    if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
                                            &Locations[Loc].line,
                                            &Locations[Loc].column, 0, 0)))
      return errorCode;
  }

  if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
                           &num_unsaved_files))
    return -1;

  CIdx = clang_createIndex(0, 1);
  TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
                                  argc - num_unsaved_files - 2 - NumLocations,
                                   argv + num_unsaved_files + 1 + NumLocations,
                                                 num_unsaved_files,
                                                 unsaved_files);
  if (!TU) {
    fprintf(stderr, "unable to parse input\n");
    return -1;
  }

  for (Loc = 0; Loc < NumLocations; ++Loc) {
    CXFile file = clang_getFile(TU, Locations[Loc].filename);
    if (!file)
      continue;

    Cursor = clang_getCursor(TU,
                             clang_getLocation(TU, file, Locations[Loc].line,
                                               Locations[Loc].column));
    PrintCursor(Cursor);
    printf("\n");
    free(Locations[Loc].filename);
  }

  PrintDiagnostics(TU);
  clang_disposeTranslationUnit(TU);
  clang_disposeIndex(CIdx);
  free(Locations);
  free_remapped_files(unsaved_files, num_unsaved_files);
  return 0;
}
Пример #7
0
int
main( int ac, char* pArgs[] )
{
	BTree_t		BTree;
	BTCursor_t	Cursor;
	BTCursor_t	Cursor1;
	BTCursor_t	Cursor2;

	NHeapPoolInit( &Pool, 1024*10, 3, malloc, free, 0/*HEAP_DYNAMIC*/ );

	BTreeInit( &BTreeMem, 2, NULL, PrintMem, BTMemAlloc, BTMemFree, NULL );
	BTreeInit( &BTree, 2, NULL, NULL, BTMalloc, BTFree, NULL );

	BTCursorOpen( &Cursor, &BTree );
	BTCursorOpen( &Cursor1, &BTree );
	BTCursorOpen( &Cursor2, &BTree );


printf("\nINSERT\n");
BTreePrint(&BTree);printf("Insert 20\n");
	BTInsert( &BTree, (void*)20 );

//	BTCFind( &Cursor1, (void*)20 );

BTreePrint(&BTree);printf("Insert 40\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)40 );
BTreePrint(&BTree);printf("Insert 10\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)10 );
BTreePrint(&BTree);printf("Insert 30\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)30 );
BTreePrint(&BTree);printf("Insert 15\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)15 );

BTreePrint(&BTree);printf("Insert 35\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)35 );
BTreePrint(&BTree);printf("Insert 7\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)7 );
BTreePrint(&BTree);printf("Insert 26\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)26 );
BTreePrint(&BTree);printf("Insert 18\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)18 );
BTreePrint(&BTree);printf("Insert 22\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)22 );

BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)5 );
printf("\nINSERT 5\n");
BTreePrint( &BTree );BTreePrint(&BTreeMem);

	BTInsert( &BTree, (void*)42 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)13 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)46 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)27 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)8 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)32 );
printf("\nINSERT 32\n");
BTreePrint( &BTree );BTreePrint(&BTreeMem);

	BTreePrint(&BTree);printf("Insert 38\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)38 );
	BTreePrint(&BTree);printf("Insert 24\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)24 );
	BTreePrint(&BTree);printf("Insert 45\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)45 );
	BTreePrint(&BTree);printf("Insert 25\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)25 );


//	Insert( &BTree, (void*)15 );
printf("\nINSERT 25\n");
BTreePrint( &BTree );BTreePrint(&BTreeMem);

//	BTCFind( &Cursor2, (void*)25 );

	BTCHead( &Cursor );
	PrintCursor( &Cursor );
	while( !BTCNext( &Cursor ) ) PrintCursor( &Cursor );

BTreePrint( &BTree );BTreePrint(&BTreeMem);
printf("\nDELETE 25\n");
	if( !BTCFind( &Cursor, (void*)25 ) )	BTCDelete( &Cursor );
	BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem);
printf("\nDELETE 45\n");
	if( !BTCFind( &Cursor, (void*)45 ) )	BTCDelete( &Cursor );
	BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem);
printf("\nDELETE 24\n");
	if( !BTCFind( &Cursor, (void*)24 ) )	BTCDelete( &Cursor );
	BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem);
printf("\nDELETE 38\n");
	if( !BTCFind( &Cursor, (void*)38 ) )	BTCDelete( &Cursor );
	BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem);

	BTDelete( &BTree, (void*)25 );
	BTDelete( &BTree, (void*)45 );
	BTDelete( &BTree, (void*)24 );

	BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTCHead( &Cursor );
	PrintCursor( &Cursor );
	while( !BTCNext( &Cursor ) ) PrintCursor( &Cursor );

printf("\nFIND 8\n");
	if( !BTCFind( &Cursor, (void*)8 ) )	PrintCursor( &Cursor);

printf("\nFIND 20<=\n");
	if( !BTCFindLower( &Cursor, (void*)20 ) )	PrintCursor( &Cursor );
	while( !BTCNext( &Cursor ) ) PrintCursor( &Cursor );

	BTDelete( &BTree, (void*)38 );
	BTDelete( &BTree, (void*)32 );

	BTDelete( &BTree, (void*)8 );
	BTDelete( &BTree, (void*)27 );
	BTDelete( &BTree, (void*)46 );
	BTDelete( &BTree, (void*)13 );
	BTDelete( &BTree, (void*)42 );

	BTDelete( &BTree, (void*)5 );
	BTDelete( &BTree, (void*)22 );
	BTDelete( &BTree, (void*)18 );
	BTDelete( &BTree, (void*)26 );

	BTDelete( &BTree, (void*)7 );
	BTDelete( &BTree, (void*)35 );
	BTDelete( &BTree, (void*)15 );

	BTreePrint( &BTree );BTreePrint(&BTreeMem);

//	BTDelete( &BTree, (void*)20 );
//	BTDelete( &BTree, (void*)40 );
//	BTDelete( &BTree, (void*)10 );
//	BTDelete( &BTree, (void*)30 );

/***
printf("END\n");
	BTreePrint( &BTree );
printf("HEAD\n");
	BTCHead( &Cursor );PrintCursor( &Cursor );
printf("NEXT\n");
	printf("%d\n",BTCDeleteAndNext( &Cursor ));PrintCursor( &Cursor );
printf("Key=%d\n", BTCGetKey( int, &Cursor ) );
***/

	BTreePrint( &BTree );
printf("=== MEMORY LEAK ===\n");
	BTreePrint( &BTreeMem );

	NHeapPoolDump( &Pool );


	BTreeInit( &BTree, 1, NULL, NULL, BMalloc, BFree, NULL );
	int	i;
	for( i = 0; i < 100; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );
	for( i = 0; i < 100; i++ ) {
		BTDelete( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );

	int	Ret;

	for( i = 0; i < 10; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );
	BTCursorOpen( &Cursor, &BTree );
	while( BTCHead( &Cursor ) == 0 ) {
		printf("DELETE(%p)\n", BTCGetKey( void*, &Cursor ) );
		Ret = BTCDelete( &Cursor );
	}
	BTCursorClose( &Cursor );
	BTreePrint( &BTree );

	BTreeInit( &BTreeMem, 3, NULL, PrintMem, BTMemAlloc, BTMemFree, NULL );
	BTreeInit( &BTree, 5, NULL, NULL, BTMalloc, BTFree, NULL );
	for( i = 206; i < 285; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );
	BTCursorOpen( &Cursor, &BTree );
	while( BTCHead( &Cursor ) == 0 ) {
		printf("DELETE(%d)\n", PNT_INT32(BTCGetKey( void*, &Cursor )) );
		Ret = BTCDelete( &Cursor );
		BTreePrint( &BTree );
	}
	BTCursorClose( &Cursor );
	BTreePrint( &BTree );
printf("=== MEMORY LEAK ===\n");
	BTreePrint( &BTreeMem );

	NHeapPoolDestroy( &Pool );
	NHeapPoolInit( &Pool, 1024*10, 10, malloc, free, 0/*HEAP_DYNAMIC*/ );
	BTreeInit( &BTreeMem, 5, NULL, NULL, BTMemAlloc, BTMemFree, NULL );
	for( i = 206; i < 285; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(i) );
	}
	BTreePrint( &BTreeMem );
	BTCursorOpen( &Cursor, &BTreeMem );
	while( BTCHead( &Cursor ) == 0 ) {
		printf("DELETE(%d)\n", PNT_INT32(BTCGetKey( void*, &Cursor )) );
		Ret = BTCDelete( &Cursor );
		BTreePrint( &BTreeMem );
	}
	BTCursorClose( &Cursor );
	BTreePrint( &BTreeMem );
//	BTreeDestroy( &BTreeMem );
	NHeapPoolDump( &Pool );

//#define	RAND_ARRAY	151
#define	RAND_ARRAY	10000
	int	aRand[RAND_ARRAY];

	for( i = 0; i < RAND_ARRAY; i++ ) {
		aRand[i]	= rand();
	}
	for( i = 0; i < RAND_ARRAY; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(aRand[i]) );
	}
	BTreePrint( &BTreeMem );

//	BTInsert( &BTreeMem, INT32_PNT(rand()) );
//	BTreePrint( &BTreeMem );

	for( i = 0; i < RAND_ARRAY; i++ ) {
		Ret = BTDelete( &BTreeMem, INT32_PNT(aRand[i]) );
		ASSERT( !Ret );
	}
	BTreePrint( &BTreeMem );
	NHeapPoolDump( &Pool );

	for( i = 0; i < RAND_ARRAY; i++ ) {
		aRand[i]	= rand();
	}
	for( i = 0; i < RAND_ARRAY; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(aRand[i]) );
	}
	for( i = 0; i < RAND_ARRAY; i++ ) {
		Ret = BTDelete( &BTreeMem, INT32_PNT(aRand[i]) );
		ASSERT( !Ret );
	}
	BTreePrint( &BTreeMem );
	NHeapPoolDump( &Pool );

	for( i = 0; i < 100; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(i) );
	}
	BTCursorOpen( &Cursor, &BTreeMem );
	for( Ret = BTCHead( &Cursor ); Ret == 0; Ret = BTCNext( &Cursor ) ) {
		i = PNT_INT32(BTCGetKey( void*, &Cursor ));
printf("%d\n", i );
		BTCDelete( &Cursor );
	}
	// Destroy
	BTreeInit( &BTree, 1, NULL, NULL, BMalloc, BFree, BDestroy );
	for( i = 0; i < 20; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );

	BTreeDestroy( &BTree );

	BTreePrint( &BTree );

	return( 0 );
}