Exemplo n.º 1
0
Cursor TranslationUnit::cursor() const
{
    return clang_getTranslationUnitCursor(cxTranslationUnit());
}
Exemplo n.º 2
0
void IndexerJob::execute()
{
    if (isAborted())
        return;
    mTimer.start();
    mData.reset(new IndexData);
    if (mType == Dump) {
        assert(id() != -1);
        if (shared_ptr<Project> p = project()) {
            for (int i=0; i<mSourceInformation.builds.size(); ++i) {
                parse(i);
                if (mUnits.at(i).second) {
                    DumpUserData u = { 0, this, !(queryFlags() & QueryMessage::NoContext) };
                    clang_visitChildren(clang_getTranslationUnitCursor(mUnits.at(i).second),
                                        IndexerJob::dumpVisitor, &u);
                }
            }
        }
    } else {
        {
            MutexLocker lock(&mMutex);
            mStarted = true;
        }
        int errorCount = 0;
        int unitCount = 0;
        const int buildCount = mSourceInformation.builds.size();
        for (int i=0; i<buildCount; ++i) {
            if (!parse(i)) {
                goto end;
            }
            if (mUnits.at(i).second)
                ++unitCount;
        }
        mParseTime = time(0);

        for (int i=0; i<buildCount; ++i) {
            int err = 0;
            if (!visit(i) || !diagnose(i, &err))
                goto end;
            errorCount += err;
        }
        {
            mData->message += mSourceInformation.sourceFile.toTilde();
            if (buildCount > 1)
                mData->message += String::format<16>(" (%d builds)", buildCount);
            if (!unitCount) {
                mData->message += " error";
            } else if (unitCount != buildCount) {
                mData->message += String::format<16>(" (%d errors, %d ok)", buildCount - unitCount, unitCount);
            }
            mData->message += String::format<16>(" in %sms. ", String::number(mTimer.elapsed()).constData());
            if (unitCount) {
                mData->message += String::format<1024>("(%d syms, %d symNames, %d refs, %d deps, %d files)",
                                                       mData->symbols.size(), mData->symbolNames.size(), mData->references.size(),
                                                       mData->dependencies.size(), mVisitedFiles.size());
            } else if (mData->dependencies.size()) {
                mData->message += String::format<16>("(%d deps)", mData->dependencies.size());
            }
            if (mType == Dirty)
                mData->message += " (dirty)";
        }
  end:
        shared_ptr<Project> p = project();
        if (p) {
            shared_ptr<IndexerJob> job = static_pointer_cast<IndexerJob>(shared_from_this());
            p->onJobFinished(job);
        }
    }
    for (int i=0; i<mUnits.size(); ++i) {
        if (mUnits.at(i).first)
            clang_disposeIndex(mUnits.at(i).first);
        if (mUnits.at(i).second)
            clang_disposeTranslationUnit(mUnits.at(i).second);
    }
    mUnits.clear();
}
Exemplo n.º 3
0
cursor translation_unit::get_cursor()
{
    return { clang_getTranslationUnitCursor(tu) };
}
Exemplo n.º 4
0
CppInstr::eErrorTypes CppInstr::parse(OovStringRef const srcFn, OovStringRef const srcRootDir,
        OovStringRef const outDir,
        char const * const clang_args[], int num_clang_args)
    {
    eErrorTypes errType = ET_None;

    mOutputFileContents.read(srcFn);
    mTopParseFn.setPath(srcFn, FP_File);
    FilePath rootDir(srcRootDir, FP_Dir);
    setFileDefine(mTopParseFn, rootDir);

    CXIndex index = clang_createIndex(1, 1);

// This doesn't appear to change anything.
//    clang_toggleCrashRecovery(true);
    // Get inclusion directives to be in AST.
    unsigned options = 0;
    CXTranslationUnit tu;
    CXErrorCode errCode = clang_parseTranslationUnit2(index, srcFn,
        clang_args, num_clang_args, 0, 0, options, &tu);
    if(errCode == CXError_Success)
        {
        CXCursor rootCursor = clang_getTranslationUnitCursor(tu);
        try
            {
            clang_visitChildren(rootCursor, ::visitTranslationUnit, this);
            }
        catch(...)
            {
            errType = ET_ParseError;
            sCrashDiagnostics.setCrashed();
            }

        std::string outFileName;
        for(int i=0; i<num_clang_args; i++)
            {
            std::string testArg = clang_args[i];
            if(testArg.compare("-o") == 0)
                {
                if(i+1 < num_clang_args)
                    outFileName = clang_args[i+1];
                }
            }
        try
            {
            mOutputFileContents.write(outFileName);
            }
        catch(...)
            {
            errType = ET_ParseError;
            }
        std::string outErrFileName = outFileName;
        outErrFileName += "-err.txt";
        size_t numDiags = clang_getNumDiagnostics(tu);
        if(numDiags > 0 || sCrashDiagnostics.hasCrashed())
            {
            FILE *fp = fopen(outErrFileName.c_str(), "w");
            if(fp)
                {
                sCrashDiagnostics.dumpCrashed(fp);
                for (size_t i = 0; i<numDiags; i++)
                    {
                    CXDiagnostic diag = clang_getDiagnostic(tu, i);
                    CXDiagnosticSeverity sev = clang_getDiagnosticSeverity(diag);
                    if(errType == ET_None || errType == ET_CompileWarnings)
                        {
                        if(sev >= CXDiagnostic_Error)
                            errType = ET_CompileErrors;
                        else
                            errType = ET_CompileWarnings;
                        }
                    CXStringDisposer diagStr = clang_formatDiagnostic(diag,
                        clang_defaultDiagnosticDisplayOptions());
                        fprintf(fp, "%s\n", diagStr.c_str());
                    }

                fprintf(fp, "Arguments: %s %s %s ", static_cast<char const *>(srcFn),
                        static_cast<char const *>(srcRootDir),
                        static_cast<char const *>(outDir));
                for(int i=0 ; i<num_clang_args; i++)
                    {
                    fprintf(fp, "%s ", clang_args[i]);
                    }
                fprintf(fp, "\n");

                fclose(fp);
                }
            }
        else
            {
            unlink(outErrFileName.c_str());
            }
        FilePath covDir(outDir, FP_Dir);
        updateCoverageHeader(mTopParseFn, covDir, mInstrCount);
        updateCoverageSource(mTopParseFn, covDir);
        }
    else
        {
        errType = ET_CLangError;
        }
    return errType;
    }
Exemplo n.º 5
0
void TokenizeSource(CXTranslationUnit tu)
{
    CXSourceRange range =
	    clang_getCursorExtent( clang_getTranslationUnitCursor(tu) );
  
    CXToken *tokens;
    unsigned int token_count;
  
    clang_tokenize( tu, range, &tokens, &token_count );

    //CXCursor cursors[ token_count ];
    //clang_annotateTokens( tu, tokens, token_count, cursors );
    auto cursors = my_annotateTokens( tu, tokens, token_count );
  
    for ( auto t = 0u; t < token_count; ++t ) {
	auto tkind = clang_getTokenKind(tokens[t] );
	auto tspelling = tkind == CXToken_Identifier ?
		CursorKindSpelling( cursors[ t ] ) :
		TokenKindSpelling( tkind );
	auto textent = clang_getTokenExtent( tu, tokens[t] );
	auto tstartloc = clang_getRangeStart( textent );
	auto tendloc = clang_getRangeEnd( textent );

	auto tokspell = clang_getTokenSpelling( tu, tokens[ t ] );
	std::cout << "TokenSpelling: " << tokspell << "\n";
	std::cout << "Cursor: " << cursors[ t ] << "\n";

	// if ( !( cursors[t].kind >= CXCursor_FirstInvalid &&
	// 	    cursors[t].kind <= CXCursor_LastInvalid ) ) {
	//   auto rr = clang_getCursorExtent( cursors[ t ] );
	//   std::cout << "Range: " << rr << "\n";
	// }
    
	// std::cout << clang_getCursorDisplayName( cursors[ t ] ) << "\n";
	// std::cout << "USR: "******"\n";

	unsigned int startoffset, endoffset;
	clang_getSpellingLocation( tstartloc, nullptr, nullptr, nullptr, &startoffset );
	clang_getSpellingLocation( tendloc, nullptr, nullptr, nullptr, &endoffset );
    
	// TODO: testing this hack for int -> identifier instead of keyword
	// but this loses const to an identifier also! fvck!
	if ( tspelling == "Keyword" ) {
	    auto type = clang_getCursorType( cursors[ t ] );
	    auto typekind = type.kind;
	    CXString typespelling;

	    if ( cursors[t].kind == CXCursor_FunctionDecl ||
		 cursors[t].kind == CXCursor_CXXMethod ) {
		type = clang_getResultType( type );
		typekind = type.kind;
		typespelling = clang_getTypeSpelling( type );
	    }
	    else
		typespelling = clang_getTypeSpelling( type );
      
	    // std::cout << "Type = " << type << " kind: " << typekind << "\n";
	    // std::cout << clang_getCString(typespelling) << " <-> " << clang_getCString(tokspell) << "\n";
	    // std::cout << " Const? " << clang_isConstQualifiedType( type ) << "\n";
      
	    if ( (( typekind >= CXType_FirstBuiltin && typekind <= CXType_LastBuiltin ) &&
		  ( std::string(clang_getCString(typespelling)) ==
		    std::string(clang_getCString(tokspell) ) )) ||
		 //	   ( cursors[t].kind == CXCursor_VarDecl ) ||
		 ( cursors[t].kind == CXCursor_ParmDecl ) )
		tspelling = "Identifier";
	}

	//if ( tspelling != "Punctuation" )
	std::cout
		<< startoffset << ":" << endoffset << " @ "
		<< tspelling  << "\n";

	clang_disposeString( tokspell );
    }

    std::cout << "\n" << end_pattern << "\n";

    clang_disposeTokens( tu, tokens, token_count );
}
bool ReflectionParser::ProcessFile(std::string const & fileName, bool InProcessModule)
{
  if (!InProcessModule && !m_sourceCache->RequestGenerate(fileName))
    return true;

  Clear();

  m_currentFile = fileName;
  m_index = clang_createIndex(true, false);

  std::vector<const char *> arguments;

  for (auto &argument : m_options.arguments)
  {
    // unescape flags
    boost::algorithm::replace_all(argument, "\\-", "-");

    arguments.emplace_back(argument.c_str());
  }

  m_translationUnit = clang_createTranslationUnitFromSourceFile(
        m_index,
        fileName.c_str(),
        static_cast<int>(arguments.size()),
        arguments.data(),
        0,
        nullptr
        );

  auto cursor = clang_getTranslationUnitCursor(m_translationUnit);

  try
  {
    Namespace tempNamespace;
    buildClasses(cursor, tempNamespace);
    tempNamespace.clear();

    if (ContainsModule() && !InProcessModule)
    {
      if (m_classes.size() > 1)
      {
        EMIT_ERROR("You can't implement any other classes in one file with module class");
      }
      return false;
    }

    if (RequestGenerate())
    {
      std::string fileId = GetFileID(fileName);
      std::stringstream outCode;

      // includes
      outCode << "#include <memory>\n\n";
      outCode << "#include \"sc-memory/cpp/sc_memory.hpp\"\n\n\n";
      outCode << "#include \"sc-memory/cpp/sc_event.hpp\"\n\n\n";

      for (auto it = m_classes.begin(); it != m_classes.end(); ++it)
      {
        Class const * klass = *it;
        if (klass->ShouldGenerate())
        {
          klass->GenerateCode(fileId, outCode, this);
        }
      }

      /// write ScFileID definition
      outCode << "\n\n#undef ScFileID\n";
      outCode << "#define ScFileID " << fileId;

      // generate output file
      boost::filesystem::path outputPath(m_options.outputPath);
      outputPath /= boost::filesystem::path(GetOutputFileName(fileName));
      std::ofstream outputFile(outputPath.string());
      outputFile << outCode.str();
      outputFile << std::endl << std::endl;
      outputFile.close();
    }

    clang_disposeIndex(m_index);
    clang_disposeTranslationUnit(m_translationUnit);

  } catch (Exception e)
  {
    clang_disposeIndex(m_index);
    clang_disposeTranslationUnit(m_translationUnit);

    EMIT_ERROR(e.GetDescription());
  }


  return true;
}
Exemplo n.º 7
0
Cursor::Cursor(TranslationUnit& unit) {
    cursor_ = clang_getTranslationUnitCursor(unit.unit_);
}