How to turn off errors/warnings in Eclipse due to

2019-04-09 17:15发布

I am using Eclipse as an editor for OpenCL and I turned on syntax highlighting for *.cl files to behave like C++ code. It works great, but all my code is underlined as syntax errors. Is there a way that I can have my syntax highlighting and turn off the errors/warnings just for my *.cl files?

2条回答
Evening l夕情丶
2楼-- · 2019-04-09 17:54

First, the Eclipse syntax highlighter is programmed to the grammar of C and C++, and not OpenCL, so it is unaware of the syntactic extensions of OpenCL, such as

  • New keywords
  • New data types

I suggest that the new keywords can be conditionally defined to nothing e.g.

#define __kernel 
#define __global

and the extra typenames can be treated similarly e.g.

#define float2 float

The #defines need guarded so as not to apply in compilation of the OpenCL code, only in the Eclipse editor. Defines can be set in the Eclipse preferences, or guarded in the kernel code itself.

#ifndef __OPENCL_VERSION__
/* Define out keywords causing errors */ 
#endif

This will have a slight problem in that it removes the distinction between overloads in functions in navigation views in Eclipse.

The ideal answer is to reprogram the CDT editor (the part of Eclipse that parses the text you type, and performs analysis on that) to be aware of OpenCL, but that would be a substantial effort.

查看更多
Explosion°爆炸
3楼-- · 2019-04-09 18:05

In addition to the answer by ggrussel I have made the following steps that give me an acceptable syntax highlighting while avoiding other problems with eclipse (tested for Kepler).

  1. Create a header file that is included in all CL files. The header file should have guarded defines for keywords and fake structures for built-in datatypes.

    #ifndef __OPENCL_VERSION__
    
    #define kernel
    #define global
    #define constant
    #define local
    
    typedef struct float2 {
      float x, y;
    } float2;
    typedef struct float3 {
      float x, y, z;
      float2 xy, xz, yx, yz, zx, zy;
    } float3;
    typedef struct float4 {
      float x, y, z, w;
      float2 xy, yx;
      float3 xyz, xzy, yxz, yzx, zxy, zyx;
    } float4;
    ... etc
    
    #endif
    

    Note that you need to define every possible combination of accesses to the primitive datatypes as if they where their own fields. Since this may become quite lengthy for the larger primites such as vec8, you may want to automatically pre-generate these fields with some script if you use such primitives.

  2. In workspace settings (Window>Preferences) add a new filetype under C/C++ > File Types. Use *.cl as the pattern and C++ Source File as type.

  3. Exclude all *.cl files from the actual build, right clicking on the file > properties > C/C++ Build > Settings > Exclude from build.

Note that after doing these changes you may need to close and re-open the CL files before the editor highlights them correctly.

An example of a CL file that compiles under OpenCL and that is correctly highlighted and shows swizzling:

#include "eclipseFakeOpenCL.h"

kernel void nextIntersect() {
  float4 a,b;

  a.xzy = (float3)(1.0, 1.0, 0.0);
  b.xy = a.yx;
}
查看更多
登录 后发表回答