This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Suppress spurious "never-assigned" warnings for globals set via '-G'


elaborate.cxx (semantic_pass_opt2): before throwing a "never-assigned global
variable" warning, search for it in 'globalopts'. If found, do not throw the
warning.
---

I came across this warning when I tried to run a script (say, stap.script):

> global traced_exec;
> 
> probe syscall.* {
>   en = execname();
>   ui = uid();
>   eui = euid();
>   if (en == traced_exec) {
>     printf("%s(%d): %s(%s)", en, pid(), name, argstr);
>     if (ui != eui) {
>       printf(" as %d/%d ", ui, eui);
>     } else {
>       printf(" as %d ", ui);
>     }
>   }
> }
> 
> probe syscall.*.return {
>   en = execname();
>   if (en == traced_exec) {
>     printf("= %s\n", retstr);
>   }
> }

- in this way:
# stap -v stap.script -G traced_exec=a.out

stap threw this spurious warning:
> WARNING: never-assigned global variable 'traced_exec' {...} at stap.script:1:8
>  source: global traced_exec;
>                 ^

This patch removes the warning in this specific case (global defined on the
command line).
---

 elaborate.cxx | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/elaborate.cxx b/elaborate.cxx
index 5d58ec7..e6d2fb1 100644
--- a/elaborate.cxx
+++ b/elaborate.cxx
@@ -3146,19 +3146,29 @@ void semantic_pass_opt2 (systemtap_session& s, bool& relaxed_p, unsigned iterati
           if (vut.written.find (l) == vut.written.end() && ! l->init) // no initializer
             if (iterations == 0 && ! s.suppress_warnings)
               {
-                set<string> vars;
-                vector<vardecl*>::iterator it;
-                for (it = s.globals.begin(); it != s.globals.end(); it++)
-                  if (l->name != (*it)->unmangled_name)
-                    if (! (*it)->unmangled_name.starts_with("__private_"))
-                      vars.insert((*it)->unmangled_name);
+                // check if it was initialized on the command line via a '-G' option
+                bool init_by_gopt = false;
+                string init_prefix (l->unmangled_name.to_string() + "=");
+                vector<std::string>::iterator gi;
+                for (gi = s.globalopts.begin(); gi != s.globalopts.end(); gi++)
+                  if (! gi->compare(0, init_prefix.size(), init_prefix))
+                    init_by_gopt = true;
+                if (! init_by_gopt)
+                  {
+                    set<string> vars;
+                    vector<vardecl*>::iterator it;
+                    for (it = s.globals.begin(); it != s.globals.end(); it++)
+                      if (l->name != (*it)->unmangled_name)
+                        if (! (*it)->unmangled_name.starts_with("__private_"))
+                          vars.insert((*it)->unmangled_name);
 
-                string sugs = levenshtein_suggest(l->name, vars, 5); // suggest top 5 vars
-                s.print_warning (_F("never-assigned global variable '%s'%s",
-                                    l->unmangled_name.to_string().c_str(),
-				    (sugs.empty() ? "" :
-				     (_(" (similar: ") + sugs + ")")).c_str()),
-				 l->tok);
+                    string sugs = levenshtein_suggest(l->name, vars, 5); // suggest top 5 vars
+                    s.print_warning (_F("never-assigned global variable '%s'%s",
+                                        l->unmangled_name.to_string().c_str(),
+                                        (sugs.empty() ? "" :
+                                         (_(" (similar: ") + sugs + ")")).c_str()),
+                                     l->tok);
+                  }
               }
 
           i++;
-- 
2.9.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]