[cmake-developers] [CMake 0015730]: foreach() mis items from list when brackets are unclosed

Mantis Bug Tracker mantis at public.kitware.com
Tue Sep 8 08:51:12 EDT 2015


The following issue has been SUBMITTED. 
====================================================================== 
http://public.kitware.com/Bug/view.php?id=15730 
====================================================================== 
Reported By:                nmeunier
Assigned To:                
====================================================================== 
Project:                    CMake
Issue ID:                   15730
Category:                   CMake
Reproducibility:            always
Severity:                   minor
Priority:                   normal
Status:                     new
====================================================================== 
Date Submitted:             2015-09-08 08:51 EDT
Last Modified:              2015-09-08 08:51 EDT
====================================================================== 
Summary:                    foreach() mis items from list when brackets are
unclosed
Description: 
I wanted to read all lines of a file, and iterate on them. But I wasn't same
account of line in my ''foreach()'' as there was in my file. The last iterated
line was like a concatenation of all remaining lines of the file. The cause is
the incorrect item was containing an opening bracket '['.

Steps to Reproduce: 
<code cmake>
function(test_list_items _nb_lines _lines)
    set(_i 0)
    message("-- test '${_lines}'")
#    string(REPLACE "]" "" _lines "${_lines}")
#    string(REPLACE "[" "" _lines "${_lines}")
    foreach (_l IN LISTS _lines)
        math(EXPR _i "${_i}+1")
        message("   ${_i}: '${_l}'")
    endforeach()
    if (NOT _i EQUAL ${_nb_lines})
        message("   ${_i} != ${_nb_lines}")
    else()
        message("   OK")
    endif()
endfunction()

set(_a "line-1;line-2\;;line-3 // [;line-4")
test_list_items(4 "${_a}")

set(_a "line-1;line-2\;;line-3 // ];line-4")
test_list_items(4 "${_a}")

set(_a "line-1;line-2\; // [;line-3 // ];line-4")
test_list_items(4 "${_a}")

set(_a "line-1;line-2\; // ];line-3 // [;line-4")
test_list_items(4 "${_a}")

set(_a "line-1;line-2\; // {;line-3 // };line-4")
test_list_items(4 "${_a}")

set(_a "line-1;line-2\;;line-3 // {;line-4")
test_list_items(4 "${_a}")

set(_a "line-1;line-2\;;line-3 // };line-4")
test_list_items(4 "${_a}")
</code>

Here is the output:
<code>
-- test 'line-1;line-2\;;line-3 // [;line-4'
   1: 'line-1'
   2: 'line-2;'
   3: 'line-3 // [;line-4'
   3 != 4
-- test 'line-1;line-2\;;line-3 // ];line-4'
   1: 'line-1'
   2: 'line-2;'
   3: 'line-3 // ];line-4'
   3 != 4
-- test 'line-1;line-2\; // [;line-3 // ];line-4'
   1: 'line-1'
   2: 'line-2; // [;line-3 // ]'
   3: 'line-4'
   3 != 4
-- test 'line-1;line-2\; // ];line-3 // [;line-4'
   1: 'line-1'
   2: 'line-2; // ];line-3 // ['
   3: 'line-4'
   3 != 4
-- test 'line-1;line-2\; // {;line-3 // };line-4'
   1: 'line-1'
   2: 'line-2; // {'
   3: 'line-3 // }'
   4: 'line-4'
   OK
-- test 'line-1;line-2\;;line-3 // {;line-4'
   1: 'line-1'
   2: 'line-2;'
   3: 'line-3 // {'
   4: 'line-4'
   OK
-- test 'line-1;line-2\;;line-3 // };line-4'
   1: 'line-1'
   2: 'line-2;'
   3: 'line-3 // }'
   4: 'line-4'
   OK
</code>

Additional Information: 
The only workaround I found is to add these transformation before the
''foreach()'':
<code cmake>
    string(REPLACE "]" "" _lines "${_lines}")
    string(REPLACE "[" "" _lines "${_lines}")
</code>
====================================================================== 

Issue History 
Date Modified    Username       Field                    Change               
====================================================================== 
2015-09-08 08:51 nmeunier       New Issue                                    
======================================================================



More information about the cmake-developers mailing list