[cfe-dev] Clang OpenCL support
Xiuxia
zhangxiuxia1 at gmail.com
Tue Sep 24 12:42:41 PDT 2013
Hi All,
Currently, I am trying to analysis OpenCL source code by using clang
AST.
I modified a sample to do this . But due to some keyword , clang doesn't
support. I
have define as the following way.
#define __global __attribute__((address_space(1)))
#define __kernel __attribute__((address_space(2)))
#define __constant __attribute__((address_space(3)))
#define CLK_LOCAL_MEM_FENCE 0
#define __local __attribute__((address_space(4)))
ahead of opencl source file before using it, otherwise it will occur some
errors.
I have set langoptions , but it seems no effect. I don't know why. Can
anyone help me fix this out ?
so that I don't have to add some defines ahead of .cl files
langOpts.OpenCL= 1
91 Invocation->setLangDefaults(langOpts,
92 clang::IK_OpenCL,
93 clang::LangStandard::lang_opencl);
This is my codes:
#include <iostream>
#include "llvm/Support/Host.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseAST.h"
/******************************************************************************
*
*****************************************************************************/
class MyASTConsumer : public clang::ASTConsumer
{
public:
MyASTConsumer() : clang::ASTConsumer() { }
virtual ~MyASTConsumer() { }
virtual bool HandleTopLevelDecl( clang::DeclGroupRef d)
{
static int count = 0;
clang::DeclGroupRef::iterator it;
for( it = d.begin(); it != d.end(); it++)
{ count++;
clang::FunctionDecl
*fd=llvm::dyn_cast<clang::FunctionDecl>(*it);
if(!fd)
{
continue;
}
if( fd->isFunctionOrMethod())
{
std::cerr << fd->getNumParams()<<"\n";
clang::ParmVarDecl* pd=fd->getParamDecl(1);
std::cerr <<
clang::QualType::getAsString(pd->getType().split()) <<"\n";
std::cerr << pd->getType().getQualifiers().getAsString()
<<"\n";
std::cerr <<
clang::QualType::getAsString(fd->getResultType().split()) <<"\n";
std::cerr << fd->getStorageClass() <<"\n";
}
}
return true;
}
};
/******************************************************************************
*
*****************************************************************************/
int main(int argc, char **argv)
{
using clang::CompilerInstance;
using clang::TargetOptions;
using clang::TargetInfo;
using clang::FileEntry;
using clang::Token;
using clang::ASTContext;
using clang::ASTConsumer;
using clang::Parser;
using clang::DiagnosticOptions;
using clang::TextDiagnosticPrinter;
using clang::CompilerInvocation;
using clang::LangOptions;
CompilerInstance ci;
DiagnosticOptions diagnosticOptions;
ci.createDiagnostics();
CompilerInvocation *Invocation = new CompilerInvocation;
ci.setInvocation(Invocation);
LangOptions langOpts;
langOpts.RTTI = 1;
langOpts.Bool = 1;
langOpts.OpenCL= 1;
Invocation->setLangDefaults(langOpts,
clang::IK_OpenCL,
clang::LangStandard::lang_opencl);
llvm::IntrusiveRefCntPtr<TargetOptions> pto( new TargetOptions());
pto->Triple = llvm::sys::getDefaultTargetTriple();
TargetInfo *pti = TargetInfo::CreateTargetInfo(ci.getDiagnostics(),
pto.getPtr());
ci.setTarget(pti);
ci.createFileManager();
ci.createSourceManager(ci.getFileManager());
ci.createPreprocessor();
ci.getPreprocessorOpts().UsePredefines = false;
MyASTConsumer *astConsumer = new MyASTConsumer();
ci.setASTConsumer(astConsumer);
ci.createASTContext();
const FileEntry *pFile = ci.getFileManager().getFile(argv[1]);
ci.getSourceManager().createMainFileID(pFile);
ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(),
&ci.getPreprocessor());
clang::ParseAST(ci.getPreprocessor(), astConsumer, ci.getASTContext());
ci.getDiagnosticClient().EndSourceFile();
return 0;
}
--
View this message in context: http://clang-developers.42468.n3.nabble.com/Clang-OpenCL-support-tp4034650.html
Sent from the Clang Developers mailing list archive at Nabble.com.
More information about the cfe-dev
mailing list