How to do a determinant in ArrayFire? - arrayfire

How can i made a simple determinant in a af::array?
Tryed to use in af::array x:
af::det(x)
det(x)
x.det()
and dont works.
Someone can help me?
error: no matching function for call to ‘det(af::array&)’
if(det(x) == 0){
candidate: template<class T> T af::det(const af::array&)
template<typename T> T det(const array &in);
^
Thanks.

According to the documentation, the function is templated. You should try something like this instead:
std::cout << af::det<float>(x);

Related

clang-8: getting typedef information from DeclRefExpr node in AST

I have below test code:
typedef void (*funcPtrType)()
funcPtrType FPT;
void myFunc(){
}
int main(){
FPT = myFunc;
FPT();
return 0;
}
And following is the part of AST dump of this code:
My question is, from which API can I get the 'void (*)()' information from DeclRefExpr node?
Already tried dynamic casting this node to VarDecl but from it I could not reach the information I mentioned.
Thanks in advance.
If you have a DeclRefExpr, that is an expression that refers to a declared entity. Call the getDecl method to get the associated ValueDecl, which is the declaration itself. On that object, call getType to get the QualType, which is the type, possibly including cv-qualifiers.
For example:
DeclRefExpr const *dre = ...; // wherever you got it
ValueDecl const *decl = dre->getDecl();
QualType type = decl->getType();
In this case, the type is a typedef. To inspect the underlying type, call getTypePtr to get the unqualified type, then getUnqualifiedDesugaredType to skip typedefs:
clang::Type const *underType = type.getTypePtr()->getUnqualifiedDesugaredType();
You can then call, for example, underType->isPointerType() to find out if it is a pointer type, etc. See the documentation for clang::Type for other ways to query it.
If you want to get a string representation of underType, use the static QualType::print method, something like this:
LangOptions lo;
PrintingPolicy pp(lo);
std::string s;
llvm::raw_string_ostream rso(s);
QualType::print(underType, Qualifiers(), rso, lo, llvm::Twine());
errs() << "type as string: \"" << rso.str() << "\"\n";
For your example, this will print:
type as string: "void (*)()"

How do I find the SourceLocation of the commas between function arguments using libtooling?

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.

Cling API available?

How to use Cling in my app via API to interpret C++ code?
I expect it to provide terminal-like way of interaction without need to compile/run executable. Let's say i have hello world program:
void main() {
cout << "Hello world!" << endl;
}
I expect to have API to execute char* = (program code) and get char *output = "Hello world!". Thanks.
PS. Something similar to ch interpeter example:
/* File: embedch.c */
#include <stdio.h>
#include <embedch.h>
char *code = "\
int func(double x, int *a) { \
printf(\"x = %f\\n\", x); \
printf(\"a[1] in func=%d\\n\", a[1]);\
a[1] = 20; \
return 30; \
}";
int main () {
ChInterp_t interp;
double x = 10;
int a[] = {1, 2, 3, 4, 5}, retval;
Ch_Initialize(&interp, NULL);
Ch_AppendRunScript(interp,code);
Ch_CallFuncByName(interp, "func", &retval, x, a);
printf("a[1] in main=%d\n", a[1]);
printf("retval = %d\n", retval);
Ch_End(interp);
}
}
There is finally a better answer: example code! See https://github.com/root-project/cling/blob/master/tools/demo/cling-demo.cpp
And the answer to your question is: no. cling takes code and returns C++ values or objects, across compiled and interpreted code. It's not a "string in / string out" kinda thing. There's perl for that ;-) This is what code in, value out looks like:
// We could use a header, too...
interp.declare("int aGlobal;\n");
cling::Value res; // Will hold the result of the expression evaluation.
interp.process("aGlobal;", &res);
std::cout << "aGlobal is " << res.getAs<long long>() << '\n';
Apologies for the late reply!
Usually the way one does it is:
[cling$] #include "cling/Interpreter/Interpreter.h"
[cling$] const char* someCode = "int i = 123;"
[cling$] gCling->declare(someCode);
[cling$] i // You will have i declared:
(int) 123
The API is documented in: http://cling.web.cern.ch/cling/doxygen/classcling_1_1Interpreter.html
Of course you can create your own 'nested' interpreter in cling's runtime too. (See the doxygen link above)
I hope it helps and answers the question, more usage examples you can find under the test/ folder.
Vassil

Recasting *void function arguments

I posted a question here earlier that I think I can answer if someone can help me with the following:
I have a function
double func(void* data)
I want to pass in an object or struct. (In my case an armadillo matrix or even just and std::vector).
How do I pass a pointer to an object as an argument to func() and then, once inside func(), how do I recast the void pointer into its original type?
Edit: Here's what ended up working, where mat is the Armadillo matrix class:
mat A(2,2);
A << 1 << 2 << endr << 3 << 4; // A=[1,2; 3,4]
func(&A);
and in func:
double func(void* data) {
mat* pB = (mat*)(data);
mat B = pB[0];
}
The matrix B and A now contain the same data.
If I understand you correctly you need something like this.
double func(void* data_v) {
struct my_type * data = data_v;
}
func((void*)my_data);

print function in python3.x?

I want to know how print function works. But I can't find print function in python3.2's source code!
In python2.5 print an integer(print 100, for example), will call int_print(). Because when create an int object, initialize PyIntObject's function pointer - tp_print that point to int_print():
printfunc tp_print;
/* We can't provide a full compile-time check that limited-API
users won't implement tp_print. However, not defining printfunc
and making tp_print of a different function pointer type
should at least cause a warning in most cases. */
typedef int (*printfunc)(PyObject *, FILE *, int);
In python3.x, all integer is an object of PyLongObject, but it initialize tp_print=NULL. And print is a function not like python2.x.
Please tell me when call print, how it works! Thanks a lot!
I have already known! When print a builtin object, print() will call builtin_print() function.
builtin_print(PyObject *self, PyObject *args, PyObject *kwds)

Resources