c++ find command no longer works - c++

This is some old code not written by me. It compiles with GCC 3.4.6, but now we are checking the build with GCC 4.4.7 and the build fails.
I hope this code is enough to go on:
list<Chapter*> * tocP; //Chapter is a class
tocP = NULL;
if (_searchChapter)
{
_chapter = _manager->GetCurrentChapter(); // _chapter is a Chapter*
}
else
{
tocP = _manager->GetTableOfContents();
if (tocP != NULL && tocP->size() > 0)
_chapter = tocP->front();
}
...
list<Chapter*>::iterator chp;
if (tocP != NULL && tocP->size() > 0)
for (chp=find(tocP->begin(),tocP->end(),_chapter); chp != tocP->end(); ++chp) // this code fails
{
//code to process chapter
}
error message is:
../src/HelpSearchC.C: In member function 'int HelpSearchC_i::DoSearch()':
../src/HelpSearchC.C:685: error: no matching function for call to 'find(std::_List_iterator<Chapter*>,
std::_List_iterator<Chapter*>, Chapter*&)'

You have to add #include <algorithm> on top of the file. The function find is defined within this header.

Related

Error while compiling with clang/clang++ [-Werror,-Wrange-loop-constrcut]

I'm trying to build a c++ project. While doing so with g++ the project compiles fine. However if I try to compile with clang I get the error:
ec_read_plan.h:135:19: error: loop variable 'op' creates a copy from type 'const std::pair<ChunkPartType, ReadPlan::ReadOperation>' [-Werror,-Wrange-loop-construct]
for (const auto op : read_operationss_{
ec_read_plan.h:135:8: note: use reference type 'const std:pair<ChunkPartType, ReadPlan::ReadOperation> &' to prevent copying
for (const auto op : read_operations) {
Code is below, I have put a comment next on the line that is giving the error:
protected:
void recoverParts(uint8_t *buffer,
const std::bitset<Goal::Slice::kMaxPartsCount> &available_parts) const {
typedef ReedSolomon<slice_traits::ec::kMaxDataCount, slice_traits::ec::kMaxParityCount> RS;
int k = slice_traits::ec::getNumberOfDataParts(slice_type);
int m = slice_traits::ec::getNumberOfParityParts(slice_type);
int max_parts = k + m;
RS::ConstFragmentMap data_parts{{0}};
RS::FragmentMap result_parts{{0}};
RS::ErasedMap erased;
RS rs(k, m);
int available_count = 0;
for (int i = 0; i < max_parts; ++i) {
if (!available_parts[i] || available_count >= k) {
erased.set(i);
} else {
available_count++;
}
}
for (const auto op : read_operations) { //ERROR appears to be here
data_parts[op.first.getSlicePart()] = buffer + op.second.buffer_offset;
}
for (int i = 0; i < (int)requested_parts.size(); ++i) {
if (!available_parts[requested_parts[i].part]) {
result_parts[requested_parts[i].part] = buffer + i * buffer_part_size;
}
}
rs.recover(data_parts, erased, result_parts, buffer_part_size);
}
Why am I getting such an error with Clang and how can I fix this? Thank you.
For anyone who may want to reproduce the error, the source code is here: https://github.com/lizardfs/lizardfs. Afterwards do:
export CC=/usr/bin/clang
export CC=/usr/bin/clang++
cd lizardfs
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/opt/lizardfs
make
It turns out the error has to do with the clang and clang++ versions. As mentioned gcc had no problems compiling. By default my machine uses clang/clang++ version 10. I tried using version 11 but also had the same problem. I therefore installed version 9 of clang/clang++ and tried compiling again and it worked.
If you happen to have the same problems make sure to run the following commands or have them in the ~/.bashrc file and run source ~/.bashrc after installing version 9 of clang/clang++
export CC=/usr/bin/clang-9
export CC=/usr/bin/clang++-9

Internal compile error c++ cilk plus

[Fixed]- Explanation given in comments
[Updated with error screenshot]
I am getting a compilation error when compiled using gcc/5.4.0. Following is the error reported:
internal compiler error: in lower_stmt, at gimple-low.c:397
cilk_spawn m_sparsify_graph_helper__(mdl, n_pa, n_ch, score2beat);
Following is the code snippet that causes error:
void m_sparsify_graph_helper__(MDL mdl, set_type pa, set_type ch, std::vector<double> score2beat) {
//cilk::reducer<cilk::op_list_append<RNode_>> rlist;
//"rlist" - defined in the class as a private variable
if (ch == 0) { return; }
set_type n_ch = ch;
// Some more code -- which I am very sure is not causing error
int lsb = n_ - 1;
for (; lsb >= 0; --lsb) { if (in_set(pa, lsb)) { break; } }
if (lsb == n_ - 1) { return; }
set_type n_pa = set_add(pa, lsb + 1);
int n_pa_sz = set_size(n_pa);
if (n_pa_sz >= n_) { return; }
BitCombination comb(n_pa, n_pa_sz, n_);
for (;;) {
n_pa = comb.data();
// If cilk_spawn keyword removed it compiles fine.
cilk_spawn m_sparsify_graph_helper__(mdl, n_pa, n_ch, score2beat);
if (!comb.next() || in_set(n_pa, n_ - 1)) { break; }
}
}// m_sparsify_graph_helper__
I assume it's a compiler error but I would like to know what is the way to circumvent this error and get the code executed warning and error free.
Error screenshot:
It seems the reported errors was ironed out in the GCC 6.X release.
FYI, if you are facing similar issue try to reproduce the error on the latest release of GCC just to confirm whether it was earlier reported and rectified or not.

Can return keyword be omitted in a return statement?

I recently come across the below piece of code in this Apache Axis tutorial example.
int main()
{
int status = AXIS2_SUCCESS;
axutil_env_t *env = NULL;
axutil_allocator_t *allocator = NULL;
env = create_environment();
status = build_and_serialize_om(env);
(status == AXIS2_FAILURE)
{
printf(" build AXIOM failed");
}
axutil_env_free(env);
0;
}
What i don't understand is the 0; at the end.
Is that return statement without the return keyword?
I tried the below piece of code to test this in Visual Studio.
int main()
{
0; // in the second run, replaced 0 with 28
}
Both programmes ran without any problems. But echo %ERRORLEVEL% at
windows command line returned 0 for both.
But the below piece of code
int add()
{
0;
}
causes
Error 1 error C4716: 'add' : must return a value
I understand that return value 0 is implicitly added for the main().
I don't have a problem including the return keyword at all, but I am
porting the Axis2/C Library to a C++ project. And there are many instances
where I encountered 0;
Why is the above syntax causing this undefined behavior?
In C++ return can be omitted only in main() , in functions that return void, and in constructors and destructors. In the former case main() returns automatically 0. In your case the statement 0; is a syntactically correct statement, evaluated as a no-op, so the compiler is basically ignoring it.
Where did you find that code? It seems like it's corrupted, perhaps due to formatting for showing it on a web page or something...?
The original code (from https://github.com/bnoordhuis/axis2-c/blob/master/axiom/test/util/axiom_util_test.c) is:
int main()
{
int status = AXIS2_SUCCESS;
axutil_env_t *env = NULL;
status = build_and_serialize_om(env);
if(status == AXIS2_FAILURE)
{
printf(" build AXIOM failed");
}
axutil_env_free(env);
return 0;
}

Getting a pointer to function in llvm

I'm trying to get a pointer to a function that returns a void and has a string as arguement. The code compiles successfully, but when running the pass, it fails with the following error:
Assertion `(i >= FTy->getNumParams() || FTy->getParamType(i) == Args[i]->getType()) && "Calling a function with a bad signature!"' failed.
Can someone please help. How do I create the signature for such a function correctly?
StructType *StructTy_class_std__basic_string = mod->getTypeByName("class.std::basic_string");
if (!StructTy_class_std__basic_string)
{
StructTy_class_std__basic_string = StructType::create(mod->getContext(), "class.std::basic_string");
}
LLVMContext &Context = mod->getContext();
Constant *c = mod->getOrInsertFunction("_Z5countSs",
Type::getVoidTy(Context),
StructTy_class_std__basic_string, NULL);
Function *funtPtr = cast<Function>(c);
And this is how I make a call to CreateCall:
Constant *a = ConstantDataArray::getString(mod->getContext(), instruction->getOpcodeName(), true);
builder.SetInsertPoint(instruction);
builder.CreateCall(count,a);

Why is this Haxe try-catch block still crashing, when using Release mode for C++ target

I have a HaxeFlixel project, that is working OK in Debug mode for misc targets, including flash, neko and windows. But Targeting Windows in Release mode, I'm having an unexpected crash, and surprisingly it's happening inside a try-catch block. Here's the crashing function:
/**
* Will safely scan a parent node's children, search for a child by name, and return it's text.
* #param parent an Fast object that is parent of the `nodeNamed` node
* #param nodeName the node's name or a comma-separated path to the child (will scan recursively)
* #return node's text as String, or null if child is not there
*/
public static function getNodeText(parent:Fast, nodeName:String):String {
try {
var _node : Fast = getNodeNamed(parent, nodeName);
//if (_node == null)
// return null;
// next line will crash if _node is null
var it :Iterator<Xml> = _node.x.iterator();
if ( it == null || !it.hasNext() )
return null;
var v = it.next();
var n = it.next();
if( n != null ) {
if( v.nodeType == Xml.PCData && n.nodeType == Xml.CData && StringTools.trim(v.nodeValue) == "" ) {
var n2 = it.next();
if( n2 == null || (n2.nodeType == Xml.PCData && StringTools.trim(n2.nodeValue) == "" && it.next() == null) )
return n.nodeValue;
}
//does not only have data (has children)
return null;
}
if( v.nodeType != Xml.PCData && v.nodeType != Xml.CData )
//does not have data";
return null;
return v.nodeValue;
}catch (err:Dynamic) {
trace("Failed parsing node Text [" + nodeName+"] " + err );
return null;
}
}
By enabling if (_node == null) return null; line, It's working safely again. By catching errors as Dynamic I thought I was supposed to catch every possible error type! Why is this happening? And why is it appearing in release mode?
My IDE is FlashDevelop, and I'm using HaxeFlixel 3.3.6, lime 0.9.7 and openFL 1.4.0, if that makes any difference
EDIT: I suspect this has to do with how the translated C++ code missed the Dynamic Exception. The equivalent generated C++ code is:
STATIC_HX_DEFINE_DYNAMIC_FUNC2(BaxXML_obj,_getNodeNamed,return )
::String BaxXML_obj::getNodeText( ::haxe::xml::Fast parent,::String nodeName){
HX_STACK_FRAME("bax.utils.BaxXML","getNodeText",0x4a152f07,"bax.utils.BaxXML.getNodeText","bax/utils/BaxXML.hx",56,0xf6e2d3cc)
HX_STACK_ARG(parent,"parent")
HX_STACK_ARG(nodeName,"nodeName")
HX_STACK_LINE(56)
try
{
HX_STACK_CATCHABLE(Dynamic, 0);
{
HX_STACK_LINE(57)
::haxe::xml::Fast _node = ::bax::utils::BaxXML_obj::getNodeNamed(parent,nodeName); HX_STACK_VAR(_node,"_node");
HX_STACK_LINE(63)
Dynamic it = _node->x->iterator(); HX_STACK_VAR(it,"it");
// ... Let's skip the irrelevant code
}
catch(Dynamic __e){
{
HX_STACK_BEGIN_CATCH
Dynamic err = __e;{
HX_STACK_LINE(82)
::String _g5 = ::Std_obj::string(err); HX_STACK_VAR(_g5,"_g5");
HX_STACK_LINE(82)
::String _g6 = (((HX_CSTRING("Failed parsing node Text [") + nodeName) + HX_CSTRING("] ")) + _g5); HX_STACK_VAR(_g6,"_g6");
HX_STACK_LINE(82)
::haxe::Log_obj::trace(_g6,hx::SourceInfo(HX_CSTRING("BaxXML.hx"),82,HX_CSTRING("bax.utils.BaxXML"),HX_CSTRING("getNodeText")));
HX_STACK_LINE(83)
return null();
}
}
}
HX_STACK_LINE(56)
return null();
}
What haxedefs do you have defined?
Adding these to your project.xml might help:
<haxedef name="HXCPP_CHECK_POINTER"/> <!--makes null references cause errors-->
<haxedef name="HXCPP_STACK_LINE" /> <!--if you want line numbers-->
<haxedef name="HXCPP_STACK_TRACE"/> <!--if you want stack traces-->
You might also try the crashdumper library:
https://github.com/larsiusprime/crashdumper
(Crashdumper will turn on HXCPP_CHECK_POINTER by default as part of it's include.xml, and will set up hooks for both hxcpp's errors and openfl/lime's uncaught error events)
I guess this boils down to how C++ handles null-pointer Exceptions. It doesn't!
More info here or here
That seems odd, some questions that may help solving it.
It looks like you are doing quite some assumptions on how the xml looks (doing some manual it.next()), why is that?
Why are you using this big-ass try-catch block?
How does getNodeNamed look, it seems it can return null.
Do you have an example xml to test with?