Beispiel #1
0
	TranslationUnit::TranslationUnit(NodeManager& mgr, const path& file, const ConversionSetup& setup)
	    : mMgr(mgr), mFileName(file), setup(setup), mClang(setup, file), mSema(mPragmaList, mClang.getPreprocessor(), mClang.getASTContext(), emptyCons, true) {
		// check for frontend extensions pragma handlers
		// and add user provided pragmas to be handled
		// by insieme
		std::map<std::string, clang::PragmaNamespace*> pragmaNamespaces;
		for(auto extension : setup.getExtensions()) {
			for(auto ph : extension->getPragmaHandlers()) {
				std::string name = ph->getName();
				// if the pragma namespace is not registered already
				// create and register it and store it in the map of pragma namespaces
				if(pragmaNamespaces.find(name) == pragmaNamespaces.end()) {
					pragmaNamespaces[name] = new clang::PragmaNamespace(name);
					mClang.getPreprocessor().AddPragmaHandler(pragmaNamespaces[name]);
				}
				// add the user provided pragma handler
				pragmaNamespaces[name]->AddPragma(pragma::PragmaHandlerFactory::CreatePragmaHandler<pragma::Pragma>(
				    mClang.getPreprocessor().getIdentifierInfo(ph->getKeyword()), *ph->getToken(), ph->getName(), ph->getFunction()));
			}
		}

		parseClangAST(mClang, &emptyCons, mSema);

		// all pragmas should now have either a decl or a stmt attached.
		// it can be the case that a pragma is at the end of a file
		// and therefore not attached to anything. Find these cases
		// and attach the pragmas to the translation unit declaration.
		for(auto cur : mPragmaList) {
			if(!cur->isStatement() && !cur->isDecl()) { cur->setDecl(getCompiler().getASTContext().getTranslationUnitDecl()); }
		}

		if(mClang.getDiagnostics().hasErrorOccurred()) {
			// errors are always fatal
			throw ClangParsingError(mFileName);
		}

		if(setup.hasOption(ConversionSetup::DumpClangAST)) {
			const std::string filter = setup.getClangASTDumpFilter();
			auto tuDecl = getASTContext().getTranslationUnitDecl();
			// if nothing defined print the whole context
			if(filter.empty()) {
				tuDecl->dumpColor();
			} else {
				// else filter out the right function decls
				auto declCtx = clang::TranslationUnitDecl::castToDeclContext(tuDecl);
				// iterate through the declarations inside and print (maybe)
				for(auto it = declCtx->decls_begin(); it != declCtx->decls_end(); ++it) {
					if(const clang::FunctionDecl* funDecl = llvm::dyn_cast<clang::FunctionDecl>(*it)) {
						if(boost::regex_match(funDecl->getNameAsString(), boost::regex(filter))) { funDecl->dumpColor(); }
					}
				}
			}
		}
	}
Beispiel #2
0
TranslationUnit::TranslationUnit(NodeManager& mgr, const path& file,  const ConversionSetup& setup)
	: mMgr(mgr), mFileName(file), setup(setup), mClang(setup, file),  
		mSema(mPragmaList, mClang.getPreprocessor(), mClang.getASTContext(), emptyCons, true) 
	{

	// check for frontend extensions pragma handlers
	// and add user provided pragmas to be handled
	// by insieme
	std::map<std::string,clang::PragmaNamespace *> pragmaNamespaces;
	for(auto extension : setup.getExtensions()) {
		for(auto ph : extension->getPragmaHandlers()) {
			std::string name = ph->getName();
			// if the pragma namespace is not registered already
			// create and register it and store it in the map of pragma namespaces
			if(pragmaNamespaces.find(name) == pragmaNamespaces.end()) {
				pragmaNamespaces[name] = new clang::PragmaNamespace(name);
				mClang.getPreprocessor().AddPragmaHandler(pragmaNamespaces[name]);
			}
			// add the user provided pragma handler
			pragmaNamespaces[name]->AddPragma(pragma::PragmaHandlerFactory::CreatePragmaHandler<pragma::Pragma>(
														mClang.getPreprocessor().getIdentifierInfo(ph->getKeyword()),
														*ph->getToken(), ph->getName(), ph->getFunction()));

		}
	}

	parseClangAST(mClang, &emptyCons, mSema);

    // all pragmas should now have either a decl or a stmt attached.
    // it can be the case that a pragma is at the end of a file
    // and therefore not attached to anything. Find these cases
    // and attach the pragmas to the translation unit declaration.
    for(auto cur : mPragmaList) {
        if(!cur->isStatement() && !cur->isDecl()) {
            cur->setDecl(getCompiler().getASTContext().getTranslationUnitDecl());
        }
    }

	if(mClang.getDiagnostics().hasErrorOccurred()) {
		// errors are always fatal
		throw ClangParsingError(mFileName);
	}
}