← Back to team overview

kicad-developers team mailing list archive

[PATCH/QUESTION] Fix -Wshadow warnings in sch_sheet_path.cpp

 

Hi,

Simple patch to fix a trio of -Wshadow warnings in SCH_SHEET_PATH.

Apparently, these shadow declarations from the STL vector header:

    /home/john/src/kicad/eeschema/sch_sheet_path.cpp: In member
function ‘void SCH_SHEET_PATH::GetComponents(SCH_REFERENCE_LIST&,
bool, bool)’:
    /home/john/src/kicad/eeschema/sch_sheet_path.cpp:202:42: warning:
declaration of ‘reference’ shadows a previous local [-Wshadow]
                     SCH_REFERENCE reference( component, part, *this );
                                              ^~~~~~~~~
    In file included from /usr/include/c++/8.1.1/vector:64,
                     from /home/john/src/kicad/include/macros.h:34,
                     from /home/john/src/kicad/eeschema/./sch_screen.h:33,
                     from /home/john/src/kicad/eeschema/sch_sheet_path.cpp:34:
    /usr/include/c++/8.1.1/bits/stl_vector.h:367:50: note: shadowed
declaration is here
           typedef typename _Alloc_traits::reference  reference;

I'm unclear on how that has come about, as I can't see anything
obviously allowing std::vector::reference to be shadowed here.

The only use of a using or typedef called "reference" seems to be in
iterator.h, but it's private and looks unrelated anyway:

    using reference = typename DLIST_ITERATOR<T>::reference;

With this code, I get no errors:

    #include <vector>

    int main() {
        int reference = 0;
        return reference;
    }

Even with this code, I don't get the same error:

    #include <vector>
    using reference = std::vector<char>::reference;

    int main() {
        int reference = 0;
        return reference;
    }

    $ g++ -Wall -Wextra -Wshadow wshadow.cpp -o wshadow.o
    wshadow.cpp: In function ‘int main()’:
    wshadow.cpp:9:9: warning: declaration of ‘reference’ shadows a
global declaration [-Wshadow]
         int reference = 0;

The patch fixes the error by renaming the offending variables to
"schReference", but I think it would be nicer to see if we can avoid
the vector typedef leaking out so it can even be shadowed in the first
place?

Compiler is GCC 8.1.1, which might have a bearing on reproducibility.

Cheers,

John
From 0a716288c7877f239c907ea360632a6778914b49 Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@xxxxxxxxx>
Date: Thu, 28 Jun 2018 14:36:14 +0100
Subject: [PATCH] Fix -Wshadow warning in sch_sheet_path.cpp
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Three instances of variables named "reference" shadow a typedef
within std::vector producing:

warning: declaration of ‘reference’ shadows a previous local [-Wshadow]
 note: shadowed declaration is here
           typedef typename _Alloc_traits::reference  reference;

This patch works around by renaming "reference" to "schReference".
---
 eeschema/sch_sheet_path.cpp | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/eeschema/sch_sheet_path.cpp b/eeschema/sch_sheet_path.cpp
index b0b34cb6b..2be088f7b 100644
--- a/eeschema/sch_sheet_path.cpp
+++ b/eeschema/sch_sheet_path.cpp
@@ -199,10 +199,10 @@ void SCH_SHEET_PATH::GetComponents( SCH_REFERENCE_LIST& aReferences, bool aInclu
 
             if( part || aForceIncludeOrphanComponents )
             {
-                SCH_REFERENCE reference( component, part, *this );
+                SCH_REFERENCE schReference( component, part, *this );
 
-                reference.SetSheetNumber( m_pageNumber );
-                aReferences.AddItem( reference );
+                schReference.SetSheetNumber( m_pageNumber );
+                aReferences.AddItem( schReference );
             }
         }
     }
@@ -229,15 +229,15 @@ void SCH_SHEET_PATH::GetMultiUnitComponents( SCH_MULTI_UNIT_REFERENCE_MAP& aRefL
 
         if( part && part->GetUnitCount() > 1 )
         {
-            SCH_REFERENCE reference = SCH_REFERENCE( component, part, *this );
-            reference.SetSheetNumber( m_pageNumber );
-            wxString reference_str = reference.GetRef();
+            SCH_REFERENCE schReference = SCH_REFERENCE( component, part, *this );
+            schReference.SetSheetNumber( m_pageNumber );
+            wxString reference_str = schReference.GetRef();
 
             // Never lock unassigned references
             if( reference_str[reference_str.Len() - 1] == '?' )
                 continue;
 
-            aRefList[reference_str].AddItem( reference );
+            aRefList[reference_str].AddItem( schReference );
         }
     }
 }
@@ -536,8 +536,8 @@ void SCH_SHEET_LIST::AnnotatePowerSymbols()
 
             if( part )
             {
-                SCH_REFERENCE reference( component, part, spath );
-                references.AddItem( reference );
+                SCH_REFERENCE schReference( component, part, spath );
+                references.AddItem( schReference );
             }
         }
     }
-- 
2.17.1


Follow ups