I had this code in one my view controller:
int randomNumber = (arc4random() % 1) + 6;
Because I will need it on more places, I decided to made it as function.
But I did it as C function, old habits die hard.
Now I have file WOC_Random.c with this content
#include <stdio.h>
#include <stdlib.h> // for arc4random() function
#ifndef WOC_Random_C
#define WOC_Random_C
int randomInt(int startInt, int endInt)
{
int randomNumber = (arc4random() % startInt) + endInt;
return randomNumber;
}
#endif
Now code in my view controller is:
int randomNumber = randomInt(1, 6);
But I have problem in linking, this is error:
duplicate symbol _randomInt in:
/Users/Mac/Library/Developer/Xcode/DerivedData/GuessTheNumber-gjovdrsarctubnbqhczqukvahwgb/Build/Intermediates/GuessTheNumber.build/Debug-iphonesimulator/GuessTheNumber.build/Objects-normal/i386/GTN_FirstViewController.o
/Users/Mac/Library/Developer/Xcode/DerivedData/GuessTheNumber-gjovdrsarctubnbqhczqukvahwgb/Build/Intermediates/GuessTheNumber.build/Debug-iphonesimulator/GuessTheNumber.build/Objects-normal/i386/WOC_Random.o
ld: 1 duplicate symbol for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I do have vague understanding, of problem.
But do not know how to fixit ?
So how to fix it, do I need some argument to linker or compiler ?
Also, in case like this when I just have some simple function to implement what is the best way to do it for iOS development, as C function or is it better to do it as class function in Object C ?
You need to add Header file (like WOC_Random.h) in which you will declare the function
int randomInt(int startInt, int endInt);
Then define that function in WOC_Random.c. And then include WOC_Random.h in the class you want to use the function.
Related
I have a C file <ask.c> :
int func(int i) {
return i;
}
int main() {
int i;
int j = func(i);
return j;
}
When I try to compiler ask.c by command <clang ask.c -Wall>, I get a warning like
ask.c:7:18: warning: variable 'i' is uninitialized when used here [-Wuninitialized]
int j = func(i);
^
ask.c:6:10: note: initialize the variable 'i' to silence this warning
int i;
^
= 0
1 warning generated.
Obviously this variable is indeed not initialized. When I went to explore how clang issued this warning, I found that it was through Diagnostics. The code of clang is as follows:
S.Diag(Use.getUser()->getBeginLoc(), diag::warn_uninit_var)
<< VD->getDeclName() << IsCapturedByBlock
<< Use.getUser()->getSourceRange();
define in Diagnostic*Kinds.td:
def warn_uninit_var : Warning<
"variable %0 is uninitialized when %select{used here|captured by block}1">,
InGroup<Uninitialized>, DefaultIgnore;
The exported information is output here, but what surprises me is that the source code of the error location is also output. How clang outputs the source code of the error location?
int j = func(i);
^
clang::Sema (your variable S in S.Diag) has access to the clang::SourceManager which in turn has access to all the raw bytes of all the input files, as well as non-files like macro expansion buffers expanded lazily if needed. The diagnostics printer uses the source manager interface to turn the clang::SourceLocation into file:line:col as well as a pointer to the raw bytes so it can print that line, as well as query the source manager to obtain the stack of macro instantiations or #include directives followed if applicable.
#include<stdio.h>
#define engine_exhaust_gas_temperature_raw 100
#define engine_exhaust_gas_temperature_scaled 20
#define Sum(x, y) ( ( x )+ ( y ) )
int main(){
printf("%d",engine_exhaust_gas_temperature_raw);
return 0;
}
I am working on MISRA C Rule 5.4 Macro identifiers shall be distinct for which I need the list of the names of all the macros defined in a C program as strings.
For ex: In the above code I will need:
[ "engine_exhaust_gas_temperature_raw", "engine_exhaust_gas_temperature_scaled", "Sum"]
Is there any way to get this list using clang AST?
I have found that we can use clangs https://clang.llvm.org/doxygen/classclang_1_1Preprocessor.html Preprocessor class to get the iterator to macros but even this is not producing any output for me. I have used it in the below code. What am I missing here?
bool distinct_macro_identifier(CompilerInstance *C_I, ASTContext *Context){
auto st= C_I->getPreprocessor().macro_begin()->getFirst()->getName();
auto x= C_I->getPreprocessor().macro_begin()->first;
llvm::outs()<<x->getName()<<"\n";
auto p= C_I->getPreprocessor().getMacroInfo(x);
p->dump();
return true;
}
You can compile it with this command:
clang++ -E -dM -nostdlib file.cpp
I want to write a configuration file with qmake that #defines a few values. But I cannot simply create variables that contain the hash or pound character (#). Nonworking example:
lines = "/* Autogenerated: do not edit */"
if(foo): lines += "#define MYLIB_WITH_FOO 1"
else: lines += "#define MYLIB_WITH_FOO 0"
write_file(config.h, lines)
The hash starts a comment (inside the string!), so this won't work. How to generate the proper #defines for write_file under qmake?
There's a predefined variable called LITERAL_HASH specially created to deal with this problem.
If this name seems too long you can create one of your own:
H = $$LITERAL_HASH
lines = "/* Autogenerated: do not edit */"
if(foo): lines += "$${H}define MYLIB_WITH_FOO 1"
else: lines += "$${H}define MYLIB_WITH_FOO 0"
write_file(config.h, lines)
The trick is to use $$system() to create the hash character. This example works for me under Windows and Linux:
pound = $$system(printf $$system_quote(\43))
if(foo): lines += "$${pound}define MYLIB_WITH_FOO 1"
else: lines += "$${pound}define MYLIB_WITH_FOO 0"
It is usual on a C or C++ application sources to include a "config.h" header, that is generated by the buildsystem from a template (for instance "config.h.in"). This is available using autotools, and also CMake - see: configure_file(). But what about Qmake?
Here is an alternative using QMAKE_SUBSTITUTES. Another reference.
test.pro
TEMPLATE = app
QT = core
CONFIG += cmdline c++11
VERSION = 1.2.3
FOO = 1
QMAKE_SUBSTITUTES = config.h.in
SOURCES += main.cpp
DISTFILES += config.h.in
config.h.in
/* Autogenerated: do not edit */
#ifndef CONFIG_H
#define CONFIG_H
#define MYLIB_VERSION '"$$VERSION"'
#define MYLIB_BANNER '"Project version $$VERSION created with Qt $$QT_VERSION"'
#define MYLIB_WITH_FOO $$FOO
#endif // CONFIG_H
main.cpp
#include <QCoreApplication>
#include <QDebug>
#include "config.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << MYLIB_BANNER;
if (MYLIB_WITH_FOO) {
qDebug() << "Foo included!";
}
return 0;
}
Output:
Project version 1.2.3 created with Qt 5.12.5
Foo included!
My main goal is trying to get macros (or even just the text) before function parameters. For example:
void Foo(_In_ void* p, _Out_ int* x, _Out_cap_(2) int* y);
I need to gracefully handle things like macros that declare parameters (by ignoring them).
#define Example _In_ int x
void Foo(Example);
I've looked at Preprocessor record objects and used Lexer::getSourceText to get the macro names In, Out, etc, but I don't see a clean way to map them back to the function parameters.
My current solution is to record all the macro expansions in the file and then compare their SourceLocation to the ParamVarDecl SourceLocation. This mostly works except I don't know how to skip over things after the parameter.
void Foo(_In_ void* p _Other_, _In_ int y);
Getting the SourceLocation of the comma would work, but I can't find that anywhere.
The title of the questions asks for libclang, but as you use Lexer::getSourceText I assume that it's libTooling. The rest of my answer is viable only in terms of libTooling.
Solution 1
Lexer works on the level of tokens. Comma is also a token, so you can take the end location of a parameter and fetch the next token using Lexer::findNextToken.
Here is a ParmVarDecl (for function parameters) and CallExpr (for function arguments) visit functions that show how to use it:
template <class T> void printNextTokenLocation(T *Node) {
auto NodeEndLocation = Node->getSourceRange().getEnd();
auto &SM = Context->getSourceManager();
auto &LO = Context->getLangOpts();
auto NextToken = Lexer::findNextToken(NodeEndLocation, SM, LO);
if (!NextToken) {
return;
}
auto NextTokenLocation = NextToken->getLocation();
llvm::errs() << NextTokenLocation.printToString(SM) << "\n";
}
bool VisitParmVarDecl(ParmVarDecl *Param) {
printNextTokenLocation(Param);
return true;
}
bool VisitCallExpr(CallExpr *Call) {
for (auto *Arg : Call->arguments()) {
printNextTokenLocation(Arg);
}
return true;
}
For the following code snippet:
#define FOO(x) int x
#define BAR float d
#define MINUS -
#define BLANK
void foo(int a, double b ,
FOO(c) , BAR) {}
int main() {
foo( 42 ,
36.6 , MINUS 10 , BLANK 0.0 );
return 0;
}
it produces the following output (six locations for commas and two for parentheses):
test.cpp:6:15
test.cpp:6:30
test.cpp:7:19
test.cpp:7:24
test.cpp:10:17
test.cpp:11:12
test.cpp:11:28
test.cpp:11:43
This is quite a low-level and error-prone approach though. However, you can change the way you solve the original problem.
Solution 2
Clang stores information about expanded macros in its source locations. You can find related methods in SourceManager (for example, isMacroArgExpansion or isMacroBodyExpansion). As the result, you can visit ParmVarDecl nodes and check their locations for macro expansions.
I would strongly advice moving in the second direction.
I hope this information will be helpful. Happy hacking with Clang!
UPD speaking of attributes, unfortunately, you won't have a lot of choices. Clang does ignore any unknown attribute and this behaviour is not tweakable. If you don't want to patch Clang itself and add your attributes to Attrs.td, then you're limited indeed to tokens and the first approach.
Can anyone help me convert following x86 inline asm code to arm format?
bool N_FLAG = 0;
bool C_FLAG = 0;
bool Z_FLAG = 0;
bool V_FLAG = 0;
asm ("sub %1, %%ebx;"\
"setsb N_FLAG;"\
"setzb Z_FLAG;"\
"setncb C_FLAG;"\
"setob V_FLAG;"\
: "=b" (reg[dest].I)\
: "r" (reg[base].I), "b" (value));
How about converting this into C?
It looks like the code subtracts two numbers (value - reg[base].I), stores the result into reg[dest].I and then checks the various flags.
So something (roughly, not tested) like:
reg[dest].I = value - reg[base].I;
Z_FLAG = (reg[dest].I == 0);
N_FLAG = (reg[dest].I < 0);
/* repeat for: carry, overflow */
And then let the compiler do its magic? The ARM gcc compiler is not bad in mapping this sort of stuff to the right ARM instructions.
If you want to go ARM assembly you're probably looking at using conditional move instructions, something like (wrote quickly - untested):
__asm__ (
"subs %[out], %[in2], %[in1]\n\t"
"movmi %[N_FLAG], #1\n\t"
"moveq %[Z_FLAG], #1\n\t"
"movcs %[C_FLAG], #1\n\t"
"movvs %[V_FLAG], #1\n\t"
: [N_FLAG]"+r"(N_FLAG), [Z_FLAG]"+r"(Z_FLAG), [C_FLAG]"+r"(C_FLAG), [V_FLAG]"+r"(V_FLAG), [out]"=r" (reg[dest].I)
: [in1]"r" (reg[base].I), [in2]"r"(value))
: "cc"
);
This code works for me using clang:
int sub(int a, int b)
{
int c;
asm ("sub %0, %2, %1" : "=r" (c) : "r" (b), "r" (a));
return c;
}
Notice the commas between the register arguments.