Clang gives confusing error message "expected value in expression" - c

I'm getting the following error message from Clang 10:
error: expected value in expression
#if FOOBAR
^
1 error generated.
No further info. What could be the cause for this?

What could be the cause for this?
When the macro is defined to nothing
#define FOOBAR
then
#if FOOBAR
expands to just:
#if
And compiler prints an error - if needs an expression #if something-here.

Related

I keep getting error when using get_string CS50

when i am using 'get_string' i am recieveing this error message. i have included <cs50.h> ! please could anyone helpas i cant figure out what I'm doing wrong
adability.c -lcrypt -lcs50 -lm -o readability
readability.c:9:19: error: initializer element is not a compile-time constant
string text = get_string ("Text: ") ;
^~~~~~~~~~~~~~~~~~~~~
/usr/include/cs50.h:109:25: note: expanded from macro 'get_string'
#define get_string(...) get_string(NULL, __VA_ARGS__)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C Error checking macro definition with function call as argument won't compile on MSVC

I'm trying to use some code that was targetted at C99 on Windows (MSVC, C11). It's some debugging macro's for OpenCL calls:
#define CL_CHECK(_expr) \
do { \
cl_int _err = _expr; \
if (_err == CL_SUCCESS) \
break; \
fprintf(stderr, "OpenCL Error: '%s' returned %d!\n", #_expr, (int)_err); \
abort(); \
} while (0)
#define CL_CHECK_ERR(_expr) \
({ \
cl_int _err = CL_INVALID_VALUE; \
typeof(_expr) _ret = _expr; \
if (_err != CL_SUCCESS) { \
fprintf(stderr, "OpenCL Error: '%s' returned %d!\n", #_expr, (int)_err); \
abort(); \
} \
_ret; \
})
The first one is used when the return type of _expr is known to be of type cl_int. The second can be of any type, and this is where I run into compilation problems. I replaced typeof(_expr) with "auto", but the compiler keeps complaining with:
Error 8 IntelliSense: expected an expression d:\work\Labs\dagSimCL\dagSimCL.cpp 136 23 dagSimCL
Error 6 error C2143: syntax error : missing ';' before '{' D:\work\Labs\dagSimCL\dagSimCL.cpp 136 1 dagSimCL
Error 7 error C2143: syntax error : missing ';' before ')' D:\work\Labs\dagSimCL\dagSimCL.cpp 136 1 dagSimCL
Error 5 error C2059: syntax error : '{' D:\work\Labs\dagSimCL\dagSimCL.cpp 136 1 dagSimCL
It doesn't really matter what I throw at the macro, it's always the same. But for reference , this is what I'm doing:
cl_context context = CL_CHECK_ERR(clCreateContext(contextProperties, 1, &devices[device_id], NULL, NULL, &_err));
Strangely enough, when I try to wrap the code in a do/while instead of () like in the first macro, it starts complaining about "do". I'm kind of at a loss here. I could just forget about it and skip the error checking, but I want to understand what is going wrong...
The CL_CHECK_EXPR() macro seems incorrect to me:
The test on _err should always fail since CL_INVALID_VALUE should be different from CL_SUCCESS.
I suppose _err should be derived from _expr, but as it is written, it is not functional.
An easy fix for the typeof lack of portability is to pass the actual type as macro argument. Inferring the type with the auto keyword as in C++ is not supported in C.
Furthermore, your compiler might not support the expression statement extension, leaving you with very few alternatives.
You should probably change the internal APIs that you want to wrap in such macros to always return an error code and use a pointer argument the return the normal result.
The CL_CHECK_EXPR() is not a valid C expression nor an initializer list to be used to the right-hand side of an assignment operator:
cl_context context = CL_CHECK_EXPR(...);
An expression in C cannot contain statements such as a compound statement (i.e. { }) nor an if statement nor a declaration statement.
You really need put that stuff inside a function that returns the desired expression and have the CL_CHECK_EXPR() macro call that function.

Preprocessor error when defining =

I was trying some awkward preprocessing and came up with something like this:
#include <stdio.h>
#define SIX =6
int main(void)
{
int x=6;
int y=2;
if(x=SIX)
printf("X == 6\n");
if(y=SIX)
printf("Y==6\n");
return 0;
}
gcc gives me the errors:
test.c: In function ‘main’:
test.c:10:8: error: expected expression before ‘=’ token
test.c:12:8: error: expected expression before ‘=’ token
Why is that?
The == is a single token, it cannot be split in half. You should run gcc -E on your code
From GCC manual pages:
-E Stop after the preprocessing stage; do not run the compiler proper. The output is in
the form of preprocessed source code, which is sent to the standard output.
Input files that don't require preprocessing are ignored.
For your code gcc -E gives the following output
if(x= =6)
printf("X == 6\n");
if(y= =6)
printf("Y==6\n");
The second = is what causes the error message expected expression before ‘=’ token
The preprocessor doesn't work at the character level, it operates at the token level. So when it performs the substitution, you get something equivalent to:
if (x = = 6)
rather than your desired:
if (x==6)
There are some specific exceptions to this, like the # stringification operator.
if(x=SIX)
is parsed as
if (x= =6).
So you get the error.
What toolchain are you using? If you are using GCC, you can add the -save-temps option and check the test.i intermediate result to troubleshoot your problem.
I suspect you have a space between the x= and the =6.

gcc c error: expected ')' before numeric constant

Hi I have been trying to port LWIP to a new arm device. When compiling the code i get the error message:
"lwip/lwip-1.4.0/src/include/lwip/memp_std.h:35:23: error: expected ')' before numeric constant"
When I go to this file this and below this several similar macros is what I find on that line:
LWIP_MEMPOOL(RAW_PCB, MEMP_NUM_RAW_PCB, sizeof(struct raw_pcb), "RAW_PCB")
If I remove the need for this macro with a define to deactivate the RAW functionality the error moves to the next LWIP_MEMPOL() macro.
The define it seems to want to put a ')' in front of is defined as this:
#define MEMP_NUM_RAW_PCB 1
The RAW_PCB is not defined but is "combined with MEMP_" to create an element in an enum.
I have tried to complie the whole ting with the -E option to get human redable object files and see if i can find any open '(' around the areas where MEMP_RAW_PCB apears and the substitution of MEMP_NUM_RAW_PCB to 1 but I have not found any by manual inspection yet.
Are there any suggestions on what can be going on here or what more I can do or look for to find the cause of the error?
I should maybe add that so far I don't call on any of the LWIP code from main() or any of the functions used in main().
I solved it with:
#ifndef MEMP_STD_H_
#define MEMP_STD_H_
... // memp_std.h codes ...
#endif //#ifndef MEMP_STD_H_
The error suggests you have unbalanced parentheses. The code you have provided thus far does not indicate where this problem is, but since ) is expected, it probably means the error is actually in the lines of code preceding the one you have shown.
Examine the code preceding the line you have shown (perhaps after using gcc -E) to check to see if all the parentheses are balanced.
If you're defining it with the dash-D option, it will generate the 1 by default, e.g.:
gcc -D 'MAX(A,B) ((A) < (B)? (B) : (A))' ...
Generates:
#define MAX(A,B) ((A) < (B)? (B) : (A)) 1
And you get the error: expected ‘)’ before numeric constant message at the line where the substitution occurs because of that trailing 1, e.g.:
int maxval = MAX(i,j);
// generates: int maxval = ((i) < (j)? (j) : (i)) 1;
Conversely, if you use the assignment operator to explicitly define the value, it will generate it the way you expected. E.g.:
int maxval = MAX(i,j);
// generates: int maxval = ((i) < (j)? (j) : (i));

error on implicit declaration, but function is declared in source file

I am trying to build a wireless driver which is eventually failing on an implicit declaration error:
wl_iw.c: In function 'wl_iw_set_priv':
wl_iw.c:7649:4: error: implicit declaration of function 'wl_iw_set_cscan' [-Werror=implicit-function-declaration]
Here is where it tries to call the function:
#if defined(CSCAN)
else if (strnicmp(extra, CSCAN_COMMAND, strlen(CSCAN_COMMAND)) == 0)
ret = wl_iw_set_cscan(dev, info, (union iwreq_data *)dwrq, extra);
#endif
So, it seems like this will only be called if CSCAN is defined. Well, in the source file, wl_iw_set_cscan is also declared if CSCAN is declared (I believe). Here is where it is defined (github), and... if you scroll up a little bit, it only seems to be dependent on CSCAN being defined.
CSCAN is definitely defined, which is shown if I do a verbose build:
arm-linux-androideabi-gcc *snip* -DCSCAN *snip* -c -o /home/owner/android-wmon/core/compat-wireless-3.6-rc7-1/drivers/net/wireless/bcmdhd/wl_iw.o /home/owner/android-wmon/core/compat-wireless-3.6-rc7-1/drivers/net/wireless/bcmdhd/wl_iw.c
I can even be doubly sure by putting a "#define CSCAN" at the top of wl_iw.c and it will complain that it is defined twice. So I'm positive that CSCAN is defined.
If this is the case, why am I getting an implicit definition warning turned error? wl_iw_set_cscan should be defined since CSCAN is defined.
At line 5781, there is another #define that is masking wl_iw_set_cscan.

Resources