clang::Token GetTokenBefore(const clang::SourceManager &sources,
                             clang::SourceLocation loc) {
   clang::Token token;
   token.setKind(clang::tok::unknown);
   clang::LangOptions lang_options;
   loc = loc.getLocWithOffset(-1);
   while (loc != sources.getLocForStartOfFile(sources.getFileID(loc))) {
     loc = clang::Lexer::GetBeginningOfToken(loc, sources, lang_options);
     if (!clang::Lexer::getRawToken(loc, token, sources, lang_options)) {
       if (!token.is(clang::tok::comment)) {
         break;
       }
     }
     loc = loc.getLocWithOffset(-1);
   }
   return token;
 }
Esempio n. 2
0
std::string MocNg::GetTag(clang::SourceLocation DeclLoc, const clang::SourceManager &SM)
{
    clang::SourceLocation FileLoc = SM.getFileLoc(DeclLoc);
    clang::FileID FID = SM.getFileID(FileLoc);
    const llvm::MemoryBuffer *Buffer = SM.getBuffer(FID);
    const char *B = Buffer->getBufferStart();
    int Off = SM.getFileOffset(FileLoc);
    int Orig = Off;
    while (Off > 0 && B[Off] != ';' && B[Off]!=',' && B[Off] != '}' && B[Off] != ':' /*&& B[Off] != '\n'*/ ) {
        Off--;
    }

    auto it_before = Tags.lower_bound(FileLoc.getLocWithOffset(Off - Orig));
    auto it_after = Tags.upper_bound(FileLoc);
    if (it_before != Tags.end() && it_after != Tags.begin() && it_before == (--it_after)) {
        return it_before->second;
    }
    return {};
}
clang::Token GetTokenAfterLocation(clang::SourceLocation loc,
                                   const clang::SourceManager &source_manager) {
  clang::Token token;
  token.setKind(clang::tok::unknown);
  clang::LangOptions lang_options;
  loc = loc.getLocWithOffset(1);
  while (loc !=
         source_manager.getLocForEndOfFile(source_manager.getFileID(loc))) {
    if (!clang::Lexer::getRawToken(loc, token, source_manager, lang_options)) {
      if (!token.is(clang::tok::comment)) {
        break;
      }
      loc = clang::Lexer::getLocForEndOfToken(token.getLocation(), /*Offset=*/0,
                                              source_manager, lang_options);
    } else {
      loc = loc.getLocWithOffset(1);
    }
  }
  return token;
}
Esempio n. 4
0
    RangeSet collect(clang::ASTContext &astContext, oclint::RuleBase *rule)
    {
        _rule = rule;
        _sourceManager = &astContext.getSourceManager();
        _range.clear();

        clang::DeclContext *decl = astContext.getTranslationUnitDecl();
        for (clang::DeclContext::decl_iterator declIt = decl->decls_begin(),
            declEnd = decl->decls_end(); declIt != declEnd; ++declIt)
        {
            clang::SourceLocation startLocation = (*declIt)->getLocStart();
            if (startLocation.isValid() &&
                _sourceManager->getMainFileID() == _sourceManager->getFileID(startLocation))
            {
                (void) /* explicitly ignore the return of this function */
                    clang::RecursiveASTVisitor<DeclAnnotationRangeCollector>::TraverseDecl(*declIt);
            }
        }

        return _range;
    }
Esempio n. 5
0
	core::annotations::Location convertClangSrcLoc(core::NodeManager& man, const clang::SourceManager& sm, clang::SourceLocation start,
	                                               clang::SourceLocation end) {
		// check file validity
		FileID&& fileId = sm.getFileID(start);
		if(fileId.isInvalid()) return core::annotations::Location::getShared();
		const clang::FileEntry* fileEntry = sm.getFileEntryForID(fileId);
		// if we cannot get the file entry, lets try to get the source filename directly
		std::string filename;
		if(!fileEntry) {
			StringRef s = sm.getFilename(start);
			filename = s.str();
		} else {
			assert_true(fileEntry);
			filename = fileEntry->getName();
		}
		// update macro locations, if required
		start = getExpansionLoc(sm, start);
		end = getExpansionLoc(sm, end);
		// generate location object
		core::IRBuilder builder(man);
		return core::annotations::Location(builder.stringValue(filename.c_str()),
		                                   core::annotations::TextPosition(sm.getSpellingLineNumber(start), sm.getSpellingColumnNumber(start)),
		                                   core::annotations::TextPosition(sm.getSpellingLineNumber(end), sm.getSpellingColumnNumber(end)));
	}