]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ir.cpp
Cleaner way to set the mask for -Wunused-component
[xonotic/gmqcc.git] / ir.cpp
diff --git a/ir.cpp b/ir.cpp
index 7ac84773830e0a148b19b1eca21268192e1fac4b..6f06bdf95e23312cb1a52cddacd99ecf8e2bc161 100644 (file)
--- a/ir.cpp
+++ b/ir.cpp
@@ -631,6 +631,36 @@ bool ir_function_finalize(ir_function *self)
     if (self->m_builtin)
         return true;
 
+    for (auto& lp : self->m_locals) {
+        ir_value *v = lp.get();
+        if (v->m_reads.empty() && v->m_writes.size() && !(v->m_flags & IR_FLAG_NOREF)) {
+            // if it's a vector check to ensure all it's members are unused before
+            // claiming it's unused, otherwise skip the vector entierly
+            if (v->m_vtype == TYPE_VECTOR)
+            {
+                size_t mask = (1 << 3) - 1, bits = 0;
+                for (size_t i = 0; i < 3; i++)
+                    if (!v->m_members[i] || (v->m_members[i]->m_reads.empty()
+                        && v->m_members[i]->m_writes.size()))
+                        bits |= (1 << i);
+                // all components are unused so just report the vector
+                if (bits == mask && irwarning(v->m_context, WARN_UNUSED_VARIABLE,
+                    "unused variable: `%s`", v->m_name.c_str()))
+                    return false;
+                else if (bits != mask)
+                    // individual components are unused so mention them
+                    for (size_t i = 0; i < 3; i++)
+                        if ((bits & (1 << i))
+                            && irwarning(v->m_context, WARN_UNUSED_COMPONENT,
+                                "unused vector component: `%s.%c`", v->m_name.c_str(), "xyz"[i]))
+                            return false;
+            }
+            // just a standard variable
+            else if (irwarning(v->m_context, WARN_UNUSED_VARIABLE,
+                    "unused variable: `%s`", v->m_name.c_str())) return false;
+        }
+    }
+
     if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
         if (!ir_function_pass_peephole(self)) {
             irerror(self->m_context, "generic optimization pass broke something in `%s`", self->m_name.c_str());